What is EOF?

Source: Internet
Author: User

Transfer from http://www.ruanyifeng.com/blog/2011/11/eof.html

When learning C language, one of the problems encountered is 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 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:

1 int C; 2 3  while ((c = fgetc (FP))! = EOF) {45 do something67 }

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:

1 int C; 2 3  while (! feof (FP)) {45 c = fgetc (FP); 6 7  Do something; 8 9 }

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:

1 intc =fgetc (FP);2 3  while(c! =EOF) {4 5  Dosomething;6c =fgetc (FP);7 8 }9 Ten if(feof (FP)) { One  Aprintf"\ n End of file reached."); -  -}Else { the  -printf"\ Something went wrong."); -  -}

In addition to representing the end of a file, EOF can also represent the end of a standard input.

1 int C; 2 3  while ((c = GetChar ())! = EOF) {45Putchar (c); 6 7 }

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.

What is EOF?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.