Feof Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business while feof c Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation feof function in c Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like while feof php you, helping each other. Join them; it only takes a minute: Sign up Why is “while ( !feof (file) )” always wrong? up vote 266 down vote favorite 135 I've seen people trying to read files like feof stdin this in a lot of posts lately. #include
Feof C++
is wrong with this while( !feof( fp )) loop? c feof share|improve this question edited Apr 7 at 21:38 asked Mar 25 '11 at 11:42 William Pursell 93k21154199 8 Consider splitting your question in 2. One part as the question proper and the other part as an answer; then put that 2nd part down there ... in the answer section of SO :) –pmg Mar 25 '11 at 11:46 11 Why it's bad to use feof() to control a loop –Grijesh Chauhan Oct 17 '13 at 18:25 Why is iostream::eof inside a loop condition considered wrong? –Jonathan Wakely Dec 4 '14 at 9:30 1 Short answer: It tries to use a status reporting function to predict whether a future operation will succeed. –David Schwartz Mar 29 at 18:26 add a comment| 7 Answers 7 active oldest votes up vote 180 down vote accepted I'd like to provide an abstract, high-level perspective. Concurrency and simultaneity I/O operations interact with the environment. The environment is not part of your program, and not under your control. The environment truly exists "concurrently" with your program. As with all things concurrent, questions about the "current state" don't make sense: There is no concept of "simultaneity" across concurrent events. Many properties of state simply don't exist concurrently. Let me make this more precise: Suppose y
Description Each FILE* that
How To Use Feof In C
you use to read and write data difference between eof and feof in c from and to a file contains flags that the system sets when ferror in c certain events occur. If you get an error, it sets the error flag; if you reach the end http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong of the file during a read, it sets the EOF flag. Pretty simple really. The functions feof() and ferror() give you a simple way to test these flags: they'll return non-zero (true) if they're set. Once the http://beej.us/guide/bgc/output/html/multipage/feof.html flags are set for a particular stream, they stay that way until you call clearerr() to clear them. Return Value feof() and ferror() return non-zero (true) if the file has reached EOF or there has been an error, respectively. Example // read binary data, checking for eof or error int main(void) { int a; FILE *fp; fp = fopen("binaryints.dat", "rb"); // read single ints at a time, stopping on EOF or error: while(fread(&a, sizeof(int), 1, fp), !feof(fp) && !ferror(fp)) { printf("I read %d\n", a); } if (feof(fp)) printf("End of file was reached.\n"); if (ferror(fp)) printf("An error occurred.\n"); fclose(fp); return 0; } See Also fopen(), fread() <
Sep 20, 2005 feof() to exit a loopYou should never use feof() as the exit indicator for a loop. feof() is TRUE only after the end of file (EOF) is read, not http://www.gidnetwork.com/b-58.html when EOF is reached. To test this, create a normal text file (mine is feof.fil):Generic Code Example: 4 Walking 3 Working 2 Eating 1 Sleeping Then compile and run this code:C/CPP/C++ Code Example: #include