End of file (EOF) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

End of file (EOF)

I'm reading The C Programming Language and have understood everything so far. However when I came across the EOF. Yet I'm confused about the condition inside of the whileloop and how EOF works. What is EOF..? What is its purpose ? When do we use it ? How Do we use it ? https://code.sololearn.com/cLc8KreR1Ony/?ref=app

10th Sep 2019, 12:03 PM
Vaibhav
Vaibhav - avatar
2 Answers
+ 5
EOF is a macro that stands for 'End of File'. It has a value -1. It is used to check whether we reached the end of the file or not. getc() function is used to read character from file. That character is then compared in the condition of while loop. If the integral value of character is non-negative, then it continues to read file else reached the end of file. Here is your code correction: #include <stdio.h> int main() { FILE *fp = fopen(__FILE__, "r"); char c = getc(fp); while(c != EOF) { putchar(c); c = getc(fp); } return 0; } __FILE__ is a macro used to refer to the current file
10th Sep 2019, 2:17 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 1
EOF is a byte symbol that means end of the file. You can use it to "cut off" any data after that symbol
10th Sep 2019, 1:33 PM
itsQuasar✅
itsQuasar✅ - avatar