Home  >  Article  >  Operation and Maintenance  >  What does linux eof mean?

What does linux eof mean?

藏色散人
藏色散人Original
2023-03-25 09:20:092517browse

linux eof generally refers to the end of the file; EOF is a computer term, the abbreviation of End Of File. In the operating system, it indicates that the data source has no more data to read. In Linux, on a new line At the beginning, press "Ctrl D" to represent EOF.

What does linux eof mean?

#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

What does linux eof mean?

EOF is a computer term, which is the abbreviation of End Of File. In the operating system, it indicates that the data source has no more data to read. Under Linux, it generally refers to the end of the file.

How to use EOF to determine whether the program has read to the end of the file?

A very important idea in the Linux system is: everything is a file. Whether it is standard input, ordinary text files in the file system, or network streams, they can be regarded as files, and they can be read and written through the read/write function. Therefore, different file types have different ways of judging whether the end of the file has been read. The following is as follows

  • Ordinary text file

  • Standard input file (stdin)

  • socket stream file

These three types of files will be introduced to determine whether they have been read to the end of the file. method.

Ordinary text files

The ordinary files here refer to those text files that we can usually see through the file manager, which exist in Linux system, and the file size is fixed.

For this kind of file, the way the Linux system determines whether an ordinary text file has been read to the end of the file is: the read function will maintain a read pointer for the opened file, and then use this pointer to determine the starting position of the file. The pointer value is subtracted to obtain an offset byte number relative to the start position of the file. Finally, such an offset byte number is compared with the size of the file itself. If the offset byte number relative to the file start position is greater than the file itself, then an EOF constant is returned, indicating that the end of the file has been read.

So, according to the above writing, if a file contains n characters, then the internal operation of the while loop will run n 1 times. Therefore, the safest way to write it is like this:

int c = fgetc(fp);
while (c != EOF) {
    do something;
    c = fgetc(fp);
}
if (feof(fp)) {
    printf("\n End of file reached.");
} else {
    printf("\n Something went wrong.");
}

Standard input file

Standard input file (stdin) corresponds to peripheral keyboard input, and in In Linux systems, it is abstracted into a file, a stream file to be precise. The biggest difference between this kind of file and the ordinary text file above is that its file size is not fixed. It is like the inlet end of a water pipe and can receive input at any time.

It is precisely because of the streaming characteristics of the standard input file that it is impossible to determine whether the end of the file has been read through the file size comparison method mentioned above. Therefore, the way the Linux system determines whether the standard input file has been read to the end of the file is to set a special input mark to represent the end of the file. In the Linux system, this mark is the key combination Ctrl D. When the system captures this key combination , let the r read function return an EOF constant to inform the program that it has read to the end of the standard file.

socket stream file

socket stream file is similar to the standard input file, and it reads data from the network, so the above two None of the methods for determining whether a file has been read to the end is applicable to socket stream files.

So how does the client process determine whether the server process has finished writing all the data?

In the socket stream file, when the client process reads the data sent by the remote server process through the read function, it uses blocking I/O to read. As long as the connection between the client and the server is not disconnected, if the server does not write data to the socket, the client's read operation will be blocked until new data is written to the server.

If the server process closes the socket connection, then the client will receive a TCP protocol FIN packet sent by the server, and then the client process is originally blocked in the read waiting to receive data from the server process. The function will be awakened at this time and return a value of 0. This is a bit different from the situation where the two types of files we mentioned earlier return EOF (value is -1) after reading the end of the file. Therefore, when reading from the socket in the program, the flag to judge the end of the data stream is not -1 but 0. .

In Linux, pressing Ctrl-D at the beginning of a new line means EOF (if you press Ctrl-D in the middle of a line, it means outputting the "standard input" buffer area, so this You must press Ctrl-D twice); in Windows, Ctrl-Z means EOF. (By the way, pressing Ctrl-Z in Linux means interrupting the process and suspending it in the background. You can use the fg command to switch back to the foreground; pressing Ctrl-C means terminating the process.)

那么,如果真的想输入Ctrl-D怎么办?这时必须先按下Ctrl-V,然后就可以输入Ctrl-D,系统就不会认为这是EOF信号。Ctrl-V表示按”字面含义”解读下一个输入,要是想按”字面含义”输入Ctrl-V,连续输入两次就行了。

所以,一个简单的从 socket 文件读取数据的样例代码,通常是下面这样的:

char recvline[MAX_LINE_LENGTH];
int read_count;
while ((read_count = read(sock_fd, recvline, MAX_LINE_LENGTH)) > 0)
{
    printf("%s\n", "String received from server: ");
    fputs(recvline, stdout);
}

总结

所以,一定要记住这样一个概念:EOF 是一个常量而不是一个字符!。

推荐学习:《linux视频教程

The above is the detailed content of What does linux eof mean?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn