怎样的feof()的作品用C [英] How feof() works in C

查看:152
本文介绍了怎样的feof()的作品用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问的feof()检查EOF为文件指针或检查位置的当前位置当前文件指针旁边?

Does feof() checks for eof for the current position of filepointer or checks for the position next to current filepointer?

感谢您的帮助!

推荐答案

每个文件流有一个内部标志,指示主叫方是否试图读取过去的结束该文件已经。 的feof 返回该标志。该标志不表示当前文件位置是否是作为文件的结尾,一个previous读只是否已试图读取过去的文件的末尾。

Every FILE stream has an internal flag that indicates whether the caller has tried to read past the end of the file already. feof returns that flag. The flag does not indicate whether the current file position is as the end of the file, only whether a previous read has tried to read past the end of the file.

作为一个例子,让我们穿行发生了什么,通过包含两个字节的文件读取时。

As an example, let's walk through what happens, when reading through a file containing two bytes.

f = fopen(filename, "r"); // file is opened
assert(!feof(f));         // eof flag is not set
c1 = getc(f);             // read first byte, one byte remaining
assert(!feof(f));         // eof flag is not set
c2 = getc(f);             // read second byte, no bytes remaining
assert(!feof(f));         // eof flag is not set
c3 = getc(f);             // try to read past end of the file
assert(feof(f));          // now, eof flag is set

这就是为什么以下是在错误的方式,通过一个文件读取时使用EOF:

This is why the following is the wrong way to use eof when reading through a file:

f = fopen(filename, "r");
while (!feof(f)) {
    c = getc(f);
    putchar(c);
}

由于的方式的feof 作品,档案结尾的标志只设置一次 GETC
试图读取过去的文件的末尾。 GETC 然后返回 EOF ,这是
没有一个字符,环路建设事业的putchar 尝试把它写
出,从而导致错误或错误的输出。

Because of the way feof works, the end-of-file flag is only set once getc tries to read past the end of the file. getc will then return EOF, which is not a character, and the loop construction causes putchar to try to write it out, resulting in an error or garbage output.

每个C标准库输入方法返回一个成功的标志或
失败: GETC 返回特殊值 EOF ,如果它试图读取过去
该文件的结尾,或者如果有一个错误,而读。特殊值
同为结束的文件和错误,而这是在哪里使用的正确方法
的feof 进来:你可以用它和错误文件结束,区分
的情况。

Every C standard library input method returns an indication of success or failure: getc returns the special value EOF if it tried to read past the end of the file, or if there was an error while reading. The special value is the same for end-of-file and error, and this is where the proper way to use feof comes in: you can use it to distinguish between end-of-file and error situations.

f = fopen(filename, "r");
c = getc(f);
if (c == EOF) {
    if (feof(f))
        printf("it was end-of-file\n");
    else
        printf("it was error\n");
}

有对错误的情况下文件对象另一个内部标志:
FERROR 。它往往是更清楚,以测试是否有错误,而不是没有结束的文件。
通过在C文件中读取一个惯用的方法是这样的:

There is another internal flag for FILE objects for error situations: ferror. It is often clearer to test for errors instead of "not end of file". An idiomatic way to read through a file in C is like this:

f = fopen(filename, "r");
while ((c = getc(f)) != EOF) {
    putchar(c);
}
if (ferror(f)) {
    perror(filename):
    exit(EXIT_FAILURE);
}
fclose(f);

(一些错误检查已经从这里省略的例子,为简洁起见。)

(Some error checking has been elided from examples here, for brevity.)

的feof 功能相当很少有用。

这篇关于怎样的feof()的作品用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆