Linux C Programming Learning--eof

Source: Internet
Author: User

EOF is the abbreviation for End of File. In the C language, it is a macro defined in the standard library. Most people think that there is an EOF in the file that represents the end of the file. But this view is actually wrong, and there is no file terminator in the data contained in the file. For getc, if it is not possible to read from a file, an integer-1 is returned, which is called EOF. The return EOF is nothing more than two cases, one is that the file has been read, and the second is the file read error, anyway, it is not read down. File Terminator Eof,windows under the combination of key Ctrl+z,unix/linux ctrl+d, in Linux, CTRL + C is the program end command is to send a kill message to the program.

First, GetChar's two points summary:

1. When you enter some valid data, add the ENTER key or Ctrl+d key GetChar to read the value from the keyboard buffer. As in the following procedure:

 while ((C=getchar ())! =EOF) {      Putchar (c)  ;  }

If you press the ENTER key to end, you will finally print the ENTER key, which is not visible and waits for the next input. If you press Ctrl+d to finish, print valid data directly and wait for the next input. When you do not enter valid data press ENTER to print out the ENTER key directly (of course the key is not visible), and again wait for the next input, directly press the CTRL+D key, the program executes the following program code, not waiting for input.

The return value of 2.getchar () is generally a character, but it can also be a negative number, which is the return of EOF. One thing to emphasize here is that the GetChar function usually returns the characters entered by the terminal, and the corresponding ASCII values in these character systems are non-negative. So, a lot of times, we'll write two lines of code like this:

Char C;  C

There is a good chance that there will be a problem. Because the GetChar function returns EOF, which is generally defined as 1 in the function library, in addition to the characters entered by the terminal, when encountering Ctrl+d (Linux), or the file terminator, EOF, is the EOF of GetChar (). Therefore, in this case, the GetChar function returns a negative value, and assigning a negative value to a variable of type char is incorrect. In order to allow the defined variable to contain all possible values returned by the GetChar function, the correct definition is as follows (this problem is specifically mentioned in K&r C):

int C;  C=getchar ();  

Ii. Two summary of EOF (mainly referred to as EOF in the normal terminal)

1.EOF as a file terminator:

Although EOF is a file terminator, it is not possible to enter Ctrl+d (Windows Ctrl + Z) in any case to achieve end-of-file functionality, only as a file terminator if the following conditions are true.

(1) When encountering Getcahr function execution, input the first character when the direct input ctrl+d, you can jump out of the GetChar (), to execute the other parts of the program;

(2) When the character entered earlier is a line break, enter ctrl+d;

(3) in the previous character input and is not a newline character, to input two times ctrl+d, then the second input ctrl+d function as a file terminator, the first ctrl+d so that GetChar began to read the data in the keyboard buffer.

In fact, these three cases can be summed up as only when the GetChar () prompt for a new input, the direct input ctrl+d is equivalent to the file terminator.

2.EOF as a line terminator, this time input ctrl+d can not end GetChar (), but only the GetChar () prompt the next round of input.

This is mainly when GetChar () a new line of input, when the input of a number of characters (cannot contain newline characters), the direct input ctrl+d, at this time the ctrl+d is not a file terminator, but just equivalent to the function of newline character, that is, the end of the current input. As an example of the above code snippet, if you enter ABC at execution time and then Ctrl+d, the program outputs the result:

Abcabc

Note: The first set of ABC is entered from the terminal, then input ctrl+d, the second set of ABC output, while the cursor stops at the second set of characters after the C, and then you can make a new input. If you enter ctrl+d again, it acts as a file terminator, ending GetChar ().

If you enter ABC and then enter, entering a newline character, the terminal appears as:

abc//first line, with carriage return

abc//Second Line

Third line

Where the first behavior terminal input, the second behavior terminal output, the cursor stops at the third line, waiting for a new terminal input.

From here you can also see the different results of the output when the Ctrl+d and newline characters are used as line terminators, respectively.

The role of EOF can also be summed up as: When the terminal has character input, the ctrl+d generated EOF equivalent to the end of the bank's input, will cause a new round of getchar () input; When the terminal does not have character input or can say when GetChar () reads a new input, the input ctrl+d, The resulting EOF is equivalent to the file terminator, and the program ends execution of GetChar ().

"Supplemental" The summary of EOF in the second part of this article applies to the terminal driver in a single row mode. That is, although GetChar () and Putchar () are indeed performed by one character at a time. But the terminal driver is in a one-line mode, and its input only ends with "\ n" or EOF, so the output from the terminal is also on line. The following program is an implementation method if you want to implement a terminal that ends typing by reading a single character.

#include <stdio.h>#include<stdlib.h>intMainvoid)   {       intC; /*The terminal driver is in a normal one-line mode*/System ("Sttyraw"); /*now the terminal driver is in one character mode at a time*/C=GetChar ();            Putchar (); /*The terminal driver goes back one line at a time*/System ("sttycooked");    Return0; }  

Compile and run the program, then when a character is entered, a character is directly sourced, and then the program ends. Thus, due to the different terminal-driven modes, the GetChar () input end condition is not the same. In normal mode, a carriage return or EOF is required, and in a single character mode, a character is entered and then the end is completed. Summary: EOF does not exist in the file, but is a state that returns this value to determine the end of the file when it is read to the end of the file or when a read error occurs. (That is, even if the read error may also be considered to be the end of the file, so you need to use feof and ferror to determine if the file is true) when using GetChar (c), even if C is defined as a character type, can also end, mainly C and-1 when compared, C will also be converted from char to an integer value. The verification program is as follows:

#include <stdio.h>    int  main ()   {      char  C;       =-1;        printf ("%x", c);       return 0 ;   }  

The result is ffffffff, so c even if it is defined as a char type, reading the file, etc., can still end normally.

Linux C Programming Learning--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.