Linux C Programming learning-EOF, linux programming eof

Source: Internet
Author: User

Linux C Programming learning-EOF, linux programming eof

EOF is the abbreviation Of End Of File. In C, it is a macro defined in the standard library. Most people think that there is an EOF in the file, which is used to indicate the end of the file. But this is actually wrong. There is no file terminator in the data contained in the file. For getc, if the object cannot be read, an integer-1 is returned, which is called EOF. The returned EOF is nothing more than two situations, one is that the file has been read; the other is that the file reading error, but it cannot be read. File Terminator EOF, Ctrl + Z in Windows, Ctrl + D in Unix/Linux, ctrl + c in linux is the end command of the program to send a kill message to the program.

I. Two points of getchar 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 shown in the following section:

while((c=getchar())!=EOF){      putchar(c);  }  

If you press enter, the enter key is printed out, which is invisible and is waiting for the next input. If you press ctrl + D, print the valid data and wait for the next input. If you do not enter valid data and press enter, the enter key is printed directly (of course this key is invisible). Then, wait for the next input and press ctrl + D, the program executes the following program code, not waiting for input.

2. the return value of getchar () is generally a character, but it may also be a negative value, that is, EOF is returned. It should be emphasized that the getchar function usually returns the characters entered by the terminal. the ASCII values in these character systems are non-negative. Therefore, we often write the following two lines of code:

char c;  c=getchar(); 

In this way, problems may occur. In addition to the characters entered by the terminal, the getchar function returns EOF when Ctrl + D (in Linux) is the file Terminator EOF, this EOF is generally defined as-1 in the function library. Therefore, in this case, the getchar function returns a negative value and it is incorrect to assign a negative value to a char variable. To allow the Defined variables to include all possible values returned by the getchar function, the correct definition method is as follows (this problem is not mentioned in K & r c ):

int c;  c=getchar();  

Ii. Two points of EOF (EOF in general terminals)

1. When EOF is used as the file Terminator:

Although EOF is a file Terminator, it does not enable the file termination function by entering Ctrl + D (Ctrl + Z in Windows) under any circumstances, is used as the file Terminator.

(1) When the getcahr function is executed, press Ctrl + D when the first character is entered to jump out of getchar () and execute other parts of the program;

(2) When the character entered earlier is a line break, press Ctrl + D;

(3) When there is a character input before and it is not a line break, enter Ctrl + D twice in a row. Then, the Ctrl + D entered for the second time serves as the end character of the file, the first Ctrl + D causes getchar to start reading data in the keyboard buffer.

In fact, all three cases can be summarized as that only when getchar () prompts a new input, directly entering Ctrl + D is equivalent to the file Terminator.

2. When EOF is used as the row Terminator, entering Ctrl + D does not end getchar (), but only triggers getchar () to prompt the next round of input.

In this case, when a new row of getchar () is input, when a certain number of characters (cannot contain line breaks) are input, press Ctrl + D, at this time, Ctrl + D is not the end of the file, but is equivalent to the line break function, that is, to end the current input. The preceding code segment is used as an example. If abc is input during execution and Ctrl + D is input, the output result of the program is:

Abcabc

Note: the first group of abc is input from the terminal, and then Ctrl + D is input, the second group of abc is output, and the cursor stops behind the second group of characters c, and then a new input can be made. If Ctrl + D is entered again, the end of the file is ended with getchar ().

If you enter abc, press enter, and enter a line break, the terminal will display:

Abc // The first line, with carriage return

Abc // The second line

// The third line

The first act is terminal input, the second act is terminal output, and the cursor stops at the third line, waiting for a new terminal input.

We can also see the different output results when Ctrl + D and linefeed are used as the line terminator respectively.

The role of EOF can also be summarized as: when there is a character input in the terminal, the EOF generated by Ctrl + D is equivalent to ending the input of this line, which will lead to a new round of input of getchar; if the terminal does not have a character input, or you can say that when getchar () reads a new input, press Ctrl + D. The generated EOF is equivalent to the file Terminator, and the program ends the execution of getchar.

[Supplement] The summary of EOF in the second part of this article is applicable to the mode where the terminal driver is in one row. That is, although getchar () and putchar () are based on one character each time. However, the terminal driver is in the one-row mode, and its input ends only when "\ n" or EOF is reached. Therefore, the output obtained on the terminal is also row-based. If you want the terminal to end the input after reading a character, the following Program is an implementation method.

# Include <stdio. h> # include <stdlib. h> int main (void) {int c;/* The Terminal Driver is in the normal one-row mode */system ("sttyraw "); /* The current terminal driver is in one character mode at a time */c = getchar (); putchar (); /* The Terminal Driver returns to the One-row mode again */system ("sttycooked"); return0 ;}

Compile and run the program. When a character is entered, the program will start with one character and end. It can be seen that the conditions for the end of the getchar () input are different due to different terminal-driven modes. In normal mode, you need to press ENTER or EOF, while in one character at a time, it will end after one character is entered. Conclusion: EOF does not exist in a file, but is a state. When you read the end Of the file or read an error, this value is returned to determine the end of the file. (That is, even if the reading error may also be considered as the end of the file, you need to use feof and ferror to determine whether the file is actually finished.) When getchar (c) is used, even if c is defined as the closed type, it can also end, mainly when comparing c and-1, c will also be converted from char to integer value. The verification procedure is as follows:

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

The result is ffffffff, so even if c is defined as char type, it can still end normally when reading files.

 

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.