C语言 C 相当于 fstream 的 peek

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2082743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:10:20  来源:igfitidea点击:

C equivalent to fstream's peek

cfilestreamstdiopeek

提问by Anthony

I know in C++, you're able to peek at the next character by using: in.peek();.

我知道在C ++中,你可以通过使用下一个字符偷看:in.peek();

How would I go about this when trying to "peek" at the next character of a file in C?

尝试在 C 中“偷看”文件的下一个字符时,我将如何处理?

回答by ephemient

fgetc+ungetc. Maybe something like this:

fgetc+ ungetc。也许是这样的:

int fpeek(FILE *stream)
{
    int c;

    c = fgetc(stream);
    ungetc(c, stream);

    return c;
}

回答by moonshadow

You could use a getcfollowed by an ungetc

您可以使用 agetc后跟ungetc

回答by Charles Ma

you'll need to implement it yourself. use freadto read the next character and fseekto go back to where you were before the read

你需要自己实现它。使用fread读取下一个字符,使用fseek返回读取前的位置

EDIT:

编辑

 int fsneaky(FILE *stream, int8_t *pBuff, int sz) {
    sz = fread(pBuff, 1, sz, stream)
    fseek(pFile, -sz, SEEK_CUR);
    return(sz);
 }