An overview of the
When we use standard C I/O, we often see the concept of EOF. It is important to clarify that there is no such character as EOF, it is just a control condition. At first, I thought EOF meant literally End of File. However, while looking at K&R recently, I found that the return of EOF indicates that the current function call has an error. These errors include:
- The file reaches the end;
- A file read/write error occurred, for example, there is no disk space.
The following is the explanation of FGETC in MAN:
fgetc() reads the next character from stream and returns it as an unsigned char cast to an int.or EOF on end of file or error.
Copy the code
Here is the definition of EOF:
#defineEOF (1)
Copy the code
Here is an example to illustrate the use of EOF.
Read a character
The fgetc function reads from the file stream one at a time.
int fgetc(FILE *stream);
Copy the code
There are a few things to note about this function:
- Each call to it returns the next character of the stream.
- The function returns an unsigned char (cast to int), which is normally an integer greater than 0.
- If the end of the file is reached or an error occurs, EOF is returned, and feof and ferror distinguish whether the end of the file is reached or an error occurred.
Here is an example of fgeTC, which reads one character from standard input and prints to standard output until 1) it reaches the end of the file; 2) Encountered an error.
#include <stdio.h>
int main(void)
{
int c;
while ((c = fgetc(stdin)) != EOF) {
fputc(c, stdout);
}
if (feof(stdin)) {
fprintf(stdout."end of file.\n");
}
if (ferror(stdin)) {
fprintf(stderr."some error ocurred.\n");
}
return 0;
}
Copy the code
lhl@ubuntu20:~/work/BLOGS/C/io$ ./eof
hello
hello
^D // Use Ctrl+D to simulate reaching the end of the file
end of file.
Copy the code
I/O EOF Unix?
How does the read function in Unix I/O indicate that a file has reached the end of the file? The value returned by man is as follows:
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
On success, the number of bytes read is returned (zero indicates end of file).and the file position is
advanced by this number. It is not an error if this number is smaller than the number of bytes requested;
this may happen forExample because fewer bytes are AC ‐tually available right now (maybe because we were
close to end-of-file, or because we are reading from a pipe, or from a terminal).or because read(a) was
interrupted by a signal. See also NOTES.
On error, -1 is returned, and errno is set appropriately. In this case, it is left unspecified whether
the file position (if any) changes.
Copy the code
Here are a few things to note:
- Read Normally, returns the number of bytes read;
- **0 indicates that the File has reached the end, Enf of File, Unix I/O EOF simply indicates that the File has reached the end.
- A return value greater than 0 but less than count is normal, indicating that this is the number of words left in the file;
- -1 indicates that the read execution encountered an error, such as a signal interrupt.
EOF in network programming
The concept of EOF is also often encountered in network programming, and specifically EOF is a condition detected by the kernel. As we saw in the previous section, when read returns 0, the end of the file is reached. Similarly, in network programming, when an application receives a value of 0 returned by read, it assumes that it has encountered the EOF condition and acts accordingly. For TCP connections, local READ signals EOF when a process actively closes one end of the connection, and the process on the other end of the connection detects EOF when it tries to read bytes after the last byte in the stream.