About the end of file

  

(1) Judging the end of the file Most people think that there is an EOF in the file to indicate the end of the file. But this view is actually wrong, there is nothing in the data contained in the file. End of file. For getc, if it can't be read from the file, it returns an integer -1, which is called EOF. Returning EOF is nothing more than two cases, one is that the file has been read; the second is the file Read error, it can't be read anymore.

(2) Why do you need feof?FILE *fp; fp points to a very complicated data structure, feof is used to determine whether the file is finished by the flag in this structure If the file is read with fgetc, the EOF flag in fp will not open when the last character is read. In this case, using feof to judge, the file will not be concluded.

fgetc -1, we still can't be sure that the file is over, because it may be a read error! At this point we need feof and feror.

(3)About Ctrl+D

getchar() The return value is normally a character, but it can also be a negative value, which returns EOF.

The point to emphasize here is that the getchar function usually returns the characters entered by the terminal. The corresponding ASCII values ​​in these character systems are non-negative. Therefore, many times, we will write two lines of code like this: char c;c =getchar();

This is likely to cause problems. Because the getchar function returns the EOF when it encounters Ctrl+D (Linux) or the end-of-file EOF, the getchar() returns EOF. This EOF is generally defined as -1 in the library. Therefore, in this case, the getchar function returns a negative value, and assigning a negative value to a char variable is incorrect. In order for the defined variable to contain all possible values ​​returned by the getchar function, the correct definition is as follows (this issue is specifically mentioned in K&RC): int c;c =getchar();

Second, the two points of EOF summary (mainly refers to EOF in the ordinary terminal) 1. EOF as the end of the file:

Although EOF is the end of the file, but not in any case, enter Ctrl+ D (Windows
Ctrl+Z) can achieve the end of the file function, only as the end of the file under the following conditions. (1) When the getcahr function is executed, when you input the first character, you can directly enter Ctrl+D, you can jump out of getchar() to execute other parts of the program; (2) when the character entered earlier is a newline character Then, type Ctrl+D; (3) When there is a character input in front and it is not a newline character, you must input Ctrl+D twice, then the second input of Ctrl+D functions as the end of file. As for the first Ctrl+D role will be introduced below. In fact, all three cases can be summarized as only when getchar() prompts a new input, directly typing Ctrl+D is equivalent to the end of file.

2. When EOF is used as a line terminator, typing Ctrl+D at this time does not end getchar(), but only getchar() prompts the next round of input.

This situation is mainly when you make a new line of input to getchar(). When you enter a number of characters (cannot contain a newline), you can directly enter Ctrl+D. At this time, Ctrl+D is not a file. The terminator is just a function equivalent to a newline character, which ends the current input. Take the above code segment as an example. If you enter abc during execution, then Ctrl+D, the program output is: abcabc

Note: The first group of abc is input from the terminal, then enter Ctrl+D, then The second set of abc is output, and the cursor is stopped behind the c of the second set of characters, and then a new input can be made. At this time, if you input Ctrl+D again, it will play the role of the end of the file and end getchar(). If you enter abc, then enter, enter a newline, then the terminal displays: abc //first line, with carriage return abc //second line //third line

where the first behavior The terminal inputs, the second acts as the terminal output, and 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 Ctrl+D and newline are used as line endings respectively. The role of EOF can also be summarized as follows: When the terminal has character input, the EOF generated by Ctrl+D is equivalent to ending the input of the line, which will cause a new round of input to getchar(); when the terminal has no character input or can be said to be getchar () When reading a new input, enter Ctrl+D. The EOF generated at this time is equivalent to the end of file, and the program will end the execution of getchar(). [Supplement] The summary part of EOF in the second part of this paper applies to the terminal driver in one line mode. That is, although getchar() and putchar() are indeed performed one character at a time. However, the terminal driver is in one line mode, and its input only ends when it is to “\ ” or EOF. Therefore, the output obtained on the terminal is also on the line. If you want to achieve the terminal to finish reading after reading a character, the following program is an implementation method (refer to "C Expert Programming", slightly modified) /*Edit by GodbachCU Blog: http://blog.chinaunix.net/u/33048/*/#include<stdio.h>#include<stdlib.h>

intmain(void){int c;/* The terminal driver is in a normal one-line mode*/system(" Stty raw");

/* The terminal driver is in one character mode at a time */c =getchar();putchar();

/* The terminal driver returns to the line one line at a time. Mode */system("sttycook");

return 0;}

Compile and run the program, when a character is entered, a character is directly used, and then the program ends. It can be seen that due to the different modes of the terminal driver, the conditions for the end of the getchar() input are different. In normal mode, a carriage return or EOF is required, and in a one-character mode, a character is entered and the game ends.

Copyright © Windows knowledge All Rights Reserved