I have managed this far with the knowledge that EOF
is a special character inserted automatically at the end of a text file to indicate its end. But I now feel the need for some more clarification on this. I checked on Google and the Wikipedia page for EOF
but they couldn't answer the following, and there are no exact Stack Overflow links for this either. So please help me on this:
My book says that binary mode files keep track of the end of file from the number of characters present in the directory entry of the file. (In contrast to text files which have a special EOF character to mark the end). So what is the story of
EOF
in context of binary files? I am confused because in the following program I successfully use!=EOF
comparison while reading from an.exe
file in binary mode:#include<stdio.h> #include<stdlib.h> int main() { int ch; FILE *fp1,*fp2; fp1=fopen("source.exe","rb"); fp2=fopen("dest.exe","wb"); if(fp1==NULL||fp2==NULL) { printf("Error opening files"); exit(-1); } while((ch=getc(fp1))!=EOF) putc(ch,fp2); fclose(fp1); fclose(fp2); }
Is
EOF
a special "character" at all? Or is it a condition as Wikipedia says, a condition where the computer knows when to return a particular value like-1
(EOF
on my computer)? Example of such "condition" being when a character-reading function finishes reading all characters present, or when character/string I/O functions encounter an error in reading/writing?Interestingly, the Stack Overflow tag for
EOF
blended both those definitions of theEOF
. The tag forEOF
said "In programming realm, EOF is a sequence of byte (or a chacracter) which indicates that there are no more contents after this.", while it also said in the "about" section that "End of file (commonly abbreviated EOF) is a condition in a computer operating system where no more data can be read from a data source. The data source is usually called a file or stream."
But I have a strong feeling EOF
won't be a character as every other function seems to be returning it when it encounters an error during I/O.
It will be really nice of you if you can clear the matter for me.