Keyboard Emulation end of file EOF

getchar () instruction

Is defined in stdio.h header file, reads a single character, including spaces, line breaks, etc. can be read. In many cases, the length of the input uncertainty, while loop is often used with getchar () of the string read. While loop end determination condition require careful consideration, reads the case getchar () instruction at the end of the main analysis file following conditions for the EOF End of File condition.

EOF

EOF is a macro definition, typically defined in stdio.h file (#define EOF -1). Mainly used to determine the end of the file. Some operating systems use Ctrl + Z character built to mark, but now there are other options such as direct recording file size information to determine whether to end.
Either way, the C language, with getchar () will return a special value EOF, scanf () function to read the same file is detected when the end of file.

Analog Test keyboard input end of file condition

But in everyday programming, sometimes need to simulate keyboard input end of the file to Windows, for example, will say a lot of information can simulate the type Crtl + Z end of file EOF. But actually run during the test, you will find does not seem so simple.
With a simple C ++ program as an example:

//EOF test
#include <iostream>
using namespace std;
int main()
{
	int testArr[100];  //EOF为-1,char一般实现为无符号,故用int数组存储
	int i = 0;
	int count = 0;  //计数器
	while ((testArr[i++] = getchar()) != EOF) {
		cout << testArr[i - 1] << " ";
		count++;
	}
	cout <<"arr[i-1]:"<< testArr[i-1] << endl;
	cout << "Total counts:"<<count;
	return 0;
}

Run shot follows :( input abc + Ctrl + Z)
(Input abc + Ctrl + Z)
Input:
A + B + C + the Ctrl + the Z
output:
97989926
expected to be output abc corresponding ASCII code, and then outputs arr [i-1]: - 1 ; a total of three cycles, output total counts: 3, and the program ends.
But after actually typing Cttl + Z, indeed immediately output, but not as expected terminate the loop, the program still waiting for the next input, while worth noting, has four output values , the first three were abc is the corresponding ASCII code, this is no problem, but how is the fourth value 26 derived?

Let us put this issue to stay behind to explain further implementation, enter Ctrl + Z again, enter:
Complete operating results

Found at this time, the end of the program actually performed, and the last element of the array is indeed valid reading is -1.

Cause Analysis

From the results of the first type Ctrl + Z began to analyze the output of the four values, obviously, Ctrl + Z were actually read, but not corresponding to -1, but the ASCII 26, ASCII code table found on the Internet 26 corresponds to:
Here Insert Picture Description
further search found that the internet has such a description:
. "in Microsoft's DOS and Windows (as well as CP / M operating system and many DEC), the terminal reads the data will not EOF in this case, the application it is a program knows the data source terminal (or other "character device"), and a character or a sequence of known retention construed as indicating the end of the file; most generally, it is the replacement characters of ASCII code (Control- Z, Code 26). Some MS-DOS programs, including some of Microsoft's MS-DOS shell (COMMAND.COM) and operating system functions (such as EDLIN), the text in the document Control-Z treated as the end of meaningful data and / or writing a text document to add to the end of the document Control-Z " 1

So, it will be understood that the source 26 is indeed because the EOF Ctrl + Z simulation, but this will not actually read and understand it to -1, but is understood to replace a character (ASCII code corresponding to 26), At the same time refresh the input buffer.

Accordingly, when the refresh buffer is about to change, is the first one encountered Ctrl + Z, this time will really be understood to EOF (-1), the loop is terminated. Verify follows: when two consecutive input Ctrl + Z When the cycle is not terminated, but before any count remains the same, and when the input buffer is first encountered when Ctrl + Z end of the file as will be appreciated, immediately end input.
Here Insert Picture Description
Here Insert Picture Description
C Primer Plus there in Chapter 8 in such a description, Sixth Edition:
. "EOF simulation concept is generated in a command line environment using a text interface in such an environment, the user interacts with the program through a keystroke by the operator EOF signal generated by the system. but in some practical applications, but it can not be converted into a good graphical interface (such as Windows and Macintosh), the user interface includes more sophisticated mouse movements and button clicks. EOF program to simulate the behavior depends on the ed
translation and item type. For example, Ctrl + Z end of the input or the entire program can, depending on the particular set. "

C ++ Primer Plus the fifth edition mentioned on page 155:
"for the PC, Microsoft Visual C ++, Borland C ++ 5.5 and GNU C ++ are able to identify the beginning of the line Ctrl + Z, but the user must then press Enter. in short, many PC environment will be considered as an analog of Ctrl + Z EOF, but the details (or the beginning of a line must be in any position, whether you must press the Enter key, etc.) vary "

Thus, on the computer I Windows10 system, the conditions in the end of the file command line window should first enter Ctrl + Z, but not necessarily immediately after to press Enter .

Finally, test it on a Linux system, also need to find the line first in order to simulate EOF, enter Ctrl + D. Inputs: A + B + C + D + the Enter the Ctrl + the Ctrl + D +
outputs as shown below:

Here Insert Picture Description
But the difference is that Windows, not in the case of analog EOF beginning of the line, pure and reading on a Linux system Ctrl + D to refresh the input buffer immediately output to the screen, but does not understand at the same time as a replacement character (ASCII code 26) .


  1. https://baike.baidu.com/item/EOF/1017800?fr=aladdin ↩︎

Published 11 original articles · won praise 1 · views 84

Guess you like

Origin blog.csdn.net/weixin_44452361/article/details/104580031