Using the feof function in C language
feof
This article will introduce several ways to use functions
in C language .
Use feof
the function to check the end-of-file indicator on a file stream in C language
feof
The function is part of the C standard input/output library and is defined in <stdio.h>
the header file. feof
The function checks the end-of-file indicator on the given file stream and EOF
returns a non-zero integer if it is set. It takes FILE
a pointer as its only argument.
In the following example, we demonstrate that when getline
reading a file line by line using the read_line function, the function is called until feof
read_line returns zero, meaning that the file stream has not yet been reached EOF
. Note that we verify the return value of the read_line function in the conditional statement and only call read_line() getline
if it succeeds .printf
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
const char* filename = "input.txt";
int main(void) {
FILE* in_file = fopen(filename, "r");
if (!in_file) {
perror("fopen");
exit(EXIT_FAILURE);
}
struct stat sb;
if (stat(filename, &sb) == -1) {
perror("stat");
exit(EXIT_FAILURE);
}
char *contents = NULL;
size_t len = 0;
while (!feof(in_file)) {
if (getline(&contents, &len, in_file) != -1) {
printf("%s", contents);
}
}
fclose(in_file);
exit(EXIT_SUCCESS);
}
feof
Use the and functions
in C ferror
to test the valid position of a file stream
Additionally, before we read the contents of a file, we can use feof
to test the position of the file stream. In this case, we fread
read the file using a call to read the file, which stat
retrieves the size of the file using the function call. Note that the buffer that stores the read bytes is malloc
allocated on the heap using the function. Additionally, we include the function in a conditional statement ferror
, EOF
along with the indicator to test the state of the file stream error
.
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
const char* filename = "input.txt";
int main(void) {
FILE* in_file = fopen(filename, "r");
if (!in_file) {
perror("fopen");
exit(EXIT_FAILURE);
}
struct stat sb;
if (stat(filename, &sb) == -1) {
perror("stat");
exit(EXIT_FAILURE);
}
char* file_contents = malloc(sb.st_size);
if (!feof(in_file) && !ferror(in_file))
fread(file_contents, 1, sb.st_size, in_file);
printf("read data: %s\n", file_contents);
free(file_contents);
fclose(in_file);
exit(EXIT_SUCCESS);
}
ferror
is also part of the I/O library and can be FILE
called on a pointer object. It returns a non-zero indicator if the error bit is set on the file stream. Note that all three examples print filename
the contents of the file specified by the variable. In the previous sample code, we used printf
the function to output the stored contents, but a more error-prone method is fwrite
to call , which can print a given number of bytes to FILE
the stream specified by the fourth parameter.
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
const char* filename = "input.txt";
int main(void) {
FILE* in_file = fopen(filename, "r");
if (!in_file) {
perror("fopen");
exit(EXIT_FAILURE);
}
struct stat sb;
if (stat(filename, &sb) == -1) {
perror("stat");
exit(EXIT_FAILURE);
}
char* file_contents = malloc(sb.st_size);
if (!feof(in_file) && !ferror(in_file))
fread(file_contents, 1, sb.st_size, in_file);
fwrite(file_contents, 1, sb.st_size, stdout);
free(file_contents);
fclose(in_file);
exit(EXIT_SUCCESS);
}
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Flushing stdout output stream in C
Publish Date:2025/04/17 Views:200 Category:C语言
-
stdout This article will demonstrate various methods on how to flush an output stream in C language . Use function in C language fflush to refresh stdout output stream I/O The C standard library provides a stdio buffered version of the I/O
在C中将整数转换为字符
Publish Date:2024/01/03 Views:135 Category:C语言
-
本教程介绍了在C中将整数转换为字符的不同方法。在C编程语言中,将整数转换为字符在各种情况下都很重要。在C中,字符是以ASCII值表示的,因此转换过程相对简单。
在 C 语言中使用 typedef enum
Publish Date:2023/05/07 Views:372 Category:C语言
-
本文介绍了如何在 C 语言中使用 typedef enum。使用 enum 在 C 语言中定义命名整数常量 enum 关键字定义了一种叫做枚举的特殊类型。
C 语言中的静态变量
Publish Date:2023/05/07 Views:170 Category:C语言
-
本文介绍了如何在 C 语言中使用静态变量。在 C 语言中使用 static 变量在函数调用之间保存变量值
C 语言中生成随机数
Publish Date:2023/05/07 Views:159 Category:C语言
-
本文演示了如何在 C 语言中生成随机数。使用 rand 和 srand 函数在 C 语言中生成随机数
C 语言中的 i++ 与++i
Publish Date:2023/05/07 Views:130 Category:C语言
-
本文演示了如何在 C 语言中使用前缀增量与后缀增量运算符。C 语言中++i 和 i++ 记号的主要区别
C 语言中获取当前工作目录
Publish Date:2023/05/07 Views:280 Category:C语言
-
本文演示了如何在 C 语言中获取当前工作目录。使用 getcwd 函数获取当前工作目录的方法
C 语言中的排序函数
Publish Date:2023/05/07 Views:212 Category:C语言
-
本文演示了如何在 C 语言中使用标准库排序函数。使用 qsort 函数对 C 语言中的整数数组进行排序