Home > Article > Backend Development > What does eof mean in c language
EOF (end of file) is a constant in the C language that represents the end of a file or stream. When using file manipulation functions, you can detect whether the end of file has been reached by checking whether the returned value is equal to EOF.
The meaning of EOF in C language
EOF (End of File) is a predefined term in C language A constant that represents the end of a file or stream. It is used to detect whether a file read or write operation has reached the end of the file.
Usage
In C language, use the EOF constant to check whether the result of an input or output operation has reached the end of file. It is often used in conjunction with file manipulation functions such as fscanf
, fprintf
, and fgetc
.
For example:
<code class="c">#include <stdio.h> int main() { FILE *fp; int c; fp = fopen("myfile.txt", "r"); while ((c = getc(fp)) != EOF) { putchar(c); } fclose(fp); return 0; }</code>
In this example, the getc()
function reads a single character from the file stream and stores it in c
in variables. The loop continues until getc()
returns EOF, indicating that the end of the file has been reached.
Notes
<code class="c">if (getc(fp) == EOF) { // 已达到文件末尾 }</code>
The opposite constant
In file operations, the opposite constant of EOF is FEOF
(File Error On End of File), which indicates that the file read or write operation failed. In most cases, FEOF is returned when a file read or write operation encounters an error (for example, the file is inaccessible).
The above is the detailed content of What does eof mean in c language. For more information, please follow other related articles on the PHP Chinese website!