C equivalent to fstream's peek

2019-01-07 22:02发布

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

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

3条回答
可以哭但决不认输i
2楼-- · 2019-01-07 22:38

fgetc+ungetc. Maybe something like this:

int fpeek(FILE *stream)
{
    int c;

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

    return c;
}
查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-07 22:46

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

EDIT:

 int fsneaky(FILE *stream, int8_t *pBuff, int sz) {
    sz = fread(pBuff, 1, sz, stream)
    fseek(pFile, -sz, SEEK_CUR);
    return(sz);
 }
查看更多
Explosion°爆炸
4楼-- · 2019-01-07 22:52

You could use a getc followed by an ungetc

查看更多
登录 后发表回答