One of the problems I encountered when I was learning C was EOF.
It is an abbreviation of end of file that represents the end of the stream. The "text stream" here can be either a file or a standard input (stdin).
For example, the following code indicates that, if it is not the end of the file, the contents of the file are copied to the screen.
int C;
while ((c = fgetc (FP))! = EOF) {
Putchar (c);
}
Naturally, I thought, at the end of each file, there is a special character called EOF, which reads to this character and the operating system thinks the file is over.
However, later I found that EOF is not a special character, but a constant defined in the header file stdio.h, which is generally equal to-1.
#define EOF (-1)
So, I was confused.
If EOF is a special character, it can be assumed that each text file has an EOF (that is, 1) at the end, because the ASCII code corresponding to the text is positive and cannot have negative values. But what about binary files? What do you do with the 1 inside the file?
This question let me think for a long time, later checked the data to know, in the Linux system, EOF is not a character at all, but when the system read to the end of the file, the return of a signal value (that is, 1). As for how the system knows the end of the file, it says by comparing the length of the file.
Therefore, the processing file can be written as follows:
int C;
while ((c = fgetc (FP))! = EOF) {
Do something
}
There is a problem with this writing. FGETC () returns EOF not only when the end of the file is encountered, but also when an error occurs. Therefore, the C language also provides the feof () function, which is used to ensure that the end of the file is indeed. The Code feof () version above is written as follows:
int C;
while (!feof (FP)) {
c = fgetc (FP);
Do something;
}
However, there are also problems with this writing. FGETC () After reading the last character of the file, the C feof () function still returns 0, indicating that the end of the file is not reached; only if FGETC () reads a character backwards (that is, over the last character), feof () returns a non-0 value indicating the end of the file.
So, in this way, if a file contains n characters, then the internal operation of the while loop runs n+1 times. So the safest way to do this is as follows:
int c = fgetc (FP);
while (c! = EOF) {
Do something;
c = fgetc (FP);
}
if (feof (FP)) {
printf ("\ n End of file reached.");
} else {
printf ("\ n Something went wrong.");
}
In addition to representing the end of a file, EOF can also represent the end of a standard input.
int C;
while ((c = GetChar ())! = EOF) {
Putchar (c);
}
However, the standard input is not the same as the file, the length of the input cannot be known beforehand, and a character must be entered manually to indicate that EOF is reached.
Linux, at the beginning of a new line, press ctrl-d, which represents EOF (if you press ctrl-d in the middle of a row, the output "standard input" buffer, so you must press two times ctrl-d); In Windows, Ctrl-z represents EOF. (Incidentally, by pressing Ctrl-z in Linux, the process is interrupted, suspended in the background, the FG command can be re-cut back to the foreground, and the process is terminated by pressing CTRL-C.) )
So, what if you really want to enter ctrl-d? At this point you must press CTRL-V, then you can enter the ctrl-d, the system will not think this is the EOF signal. Ctrl-v said that by "literal meaning" to interpret the next input, if you want to press "literal meaning" input ctrl-v, input two consecutive times on the line.
Http://www.ruanyifeng.com/blog/2011/11/eof.html
What is EOF?