C equivalent to fstream's peek

你说的曾经没有我的故事 提交于 2019-11-26 11:26:01

问题


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?


回答1:


fgetc+ungetc. Maybe something like this:

int fpeek(FILE *stream)
{
    int c;

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

    return c;
}



回答2:


You could use a getc followed by an ungetc




回答3:


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);
 }


来源:https://stackoverflow.com/questions/2082743/c-equivalent-to-fstreams-peek

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!