Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
666 views
in Technique[技术] by (71.8m points)

file handling - How feof() works in C

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

Thanks for your help !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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.

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
");
    else
        printf("it was error
");
}

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.)

The feof function is fairly rarely useful.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...