C 文件处理中的 feof()

发布于 2024-08-28 23:24:35 字数 402 浏览 3 评论 0 原文

我正在逐字节读取二进制文件,我需要确定 eof 是否已到达。

feof() 不起作用,因为“仅当发出对不存在字节的读取请求时才设置 eof”。所以,我可以像这样自定义 check_eof:

if ( fread(&byte,sizeof(byte),1,fp) != 1) {
    if(feof(fp))
    return true;
}
return false;

但问题是,如果未达到 eof,我的文件指针会向前移动一个字节。 因此,解决方案可能是使用 ftell() 然后使用 fseek() 将其定位到正确的位置。

另一种解决方案可能是将前面的字节缓冲在某个临时存储中。

还有更好的解决方案吗?

I am reading a binary file byte-by-byte,i need determine that whether or not eof has reached.

feof() doesn't works as "eof is set only when a read request for non-existent byte is made". So, I can have my custom check_eof like:

if ( fread(&byte,sizeof(byte),1,fp) != 1) {
    if(feof(fp))
    return true;
}
return false;

But the problem is, in case when eof is not reached, my file pointer is moved a byte ahead.
So a solution might be to use ftell() and then fseek() to get it to correct position.

Another solution might be to buffer the byte ahead in some temporary storage.

Any better solutions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深居我梦 2024-09-04 23:24:36

如果您一次读取一个字节,则惯用的方法是使用 fgetc:

int c;
while ((c = fgetc(fp)) != EOF) {
   // Do something.
}

然后您不需要处理 feof

If you're reading a byte at a time, the idiomatic way to do this is with fgetc:

int c;
while ((c = fgetc(fp)) != EOF) {
   // Do something.
}

and then you shouldn't need to deal with feof.

装纯掩盖桑 2024-09-04 23:24:36

我通常会做这样的事情:

int get_next_char(FILE* fp, char *ch)
{
    return fread(ch, sizeof(char),1, fp) == 1;
}

// main loop
char ch;
while (get_next_char(fp, &ch))
    process_char(ch);

if (!feof(fp))
    handle_unexpected_input_error(fp);

I typically do something like this:

int get_next_char(FILE* fp, char *ch)
{
    return fread(ch, sizeof(char),1, fp) == 1;
}

// main loop
char ch;
while (get_next_char(fp, &ch))
    process_char(ch);

if (!feof(fp))
    handle_unexpected_input_error(fp);
冰雪之触 2024-09-04 23:24:36

最好构建您的代码,以便您尝试读取一些数据,如果由于到达文件末尾而导致读取失败,您可以在那里处理它(即参见 克里斯托弗·约翰逊的回答)。

如果你绝对讨厌这个,你可以使用 ungetc 将单个字符返回到流中,并且它将在下一个读取调用中可用:

int c = fgetc(fp);
if (c == EOF)
{
    // handle eof / error
}
else
{
    ungetc(c, fp);

    // the next read call is guaranteed to return at least one byte

}

It's best to structure your code so that you try to read some data and if the read does not succeed due to reaching the end-of-file, you deal with it there (i.e. see Kristopher Johnson's answer).

If you absolutely hate this, you can use ungetc to return a single character back to the stream and it will be available in the next read call:

int c = fgetc(fp);
if (c == EOF)
{
    // handle eof / error
}
else
{
    ungetc(c, fp);

    // the next read call is guaranteed to return at least one byte

}
乖乖 2024-09-04 23:24:36

我不清楚,但如果您有兴趣在读取字节之前知道是否已达到 EOF,请将 feof() 测试放在 fread() 之前而不是之后。
实际上,如果我没读错的话,你甚至不想这样做:

return feof(fp) ? true : false;

I'm not clear, but if you're interested in knowing before you read the byte if EOF has been reached, put your feof() test before the fread() rather than after it.
actually, if I read correctly, you don't even want to do the fread so:

return feof(fp) ? true : false;
凉世弥音 2024-09-04 23:24:36

我建议使用:

fseek(myFile, 0, SEEK_END);
fileLen = ftell(myFile);
fseek(myfile, 0, SEEK_SET);

当您第一次打开文件时确定长度。然后将你的读循环设置为永远不会读到末尾。您可以使用 ftell 来确定您所在的位置,并与 fileLen 进行比较来确定您还需要走多远。

I would suggest using:

fseek(myFile, 0, SEEK_END);
fileLen = ftell(myFile);
fseek(myfile, 0, SEEK_SET);

When you first open the file to determine the length. Then set up your read-loop to never read off the end. You can use ftell to figure out where you are, and compare to fileLen to figure out how much further you have to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文