C program end of file
A newline which is what happens when you press enter isn't the end of a file , it's the end of a line , so a newline doesn't terminate this loop. It reads to the end of the input, but you seem to want to read only to the end of a line. The value of EOF is -1 because it has to be different from any return value from getchar that is an actual character. So getchar returns any character value as an unsigned char, converted to int, which will therefore be non-negative.
Then after all the input has been read, getchar will return EOF , and hence getchar! EOF is -1 because that's how it's defined.
The name is provided by the standard library headers that you include. They make it equal to -1 because it has to be something that can't be mistaken for an actual byte read by getchar. So what happens is, we call the getchar function, and compare the result to -1 EOF.
If the result was not equal to EOF, then the result is true, because things that are not equal are not equal. If the result was equal to EOF, then the result is false, because things that are equal are not not equal. The call to getchar returns EOF when you reach the "end of file". As far as C is concerned, the 'standard input' the data you are giving to your program by typing in the command window is just like a file.
Of course, you can always type more, so you need an explicit way to say "I'm done". On Windows systems, this is control-Z. On Unix systems, this is control-D. The example in the book is not "wrong". It depends on what you actually want to do.
Reading until EOF means that you read everything, until the user says "I'm done", and then you can't read any more. Stack Overflow for Teams — Collaborate and share knowledge with a private group. This is the wrong way to read a file: while! Martin York Martin York k 80 80 gold badges silver badges bronze badges.
After reading the last number in the file or after trying to read the first number which does not exist? Same question for fin. Reading upto the end of file does not change the state. So EOF becomes true when you read the first value that does not exist past the end of file. Thanks again! For ifstream::fail , does it become true when first reading past the end of the file just as eof does?
In my example, it seems to be true. But I learned from cplusplus. At least one of these flags is set when some error other than reaching the End-Of-File occurs during an input operation". Does it say fail doesn't become true only because EOF occurs? The fail bit is set if the last operation failed. In this case the fail and eof will become true at the same time. But this does not imply that fail and eof are the same thing. At this point the fail bit is true but eof is still false.
Show 6 more comments. Andre Andre 94 3 3 bronze badges. I tried the first case, and it is not correct that " 4 will return true". Sign up or log in Sign up using Google. Sign up using Facebook. So, only comparing the value returned by getc with EOF is not sufficient to check for actual end of file. To solve this problem, C provides feof which returns non-zero value only if end of file has reached, otherwise it returns 0.
For example, consider the following C program to print contents of file test. In the program, returned value of getc is compared with EOF first, then there is another check using feof. Take a step-up from those "Hello World" programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.
Previous fseek vs rewind in C. Next fopen for an existing file in write mode.
0コメント