JIYIK CN >

Current Location:Home > Learning > PROGRAM > C语言 >

Using the feof function in C language

Author:JIYIK Last Updated:2025/04/17 Views:

feofThis article will introduce several ways to use functions in C language .


Use feofthe function to check the end-of-file indicator on a file stream in C language

feofThe function is part of the C standard input/output library and is defined in <stdio.h>the header file. feofThe function checks the end-of-file indicator on the given file stream and EOFreturns a non-zero integer if it is set. It takes FILEa pointer as its only argument.

In the following example, we demonstrate that when getlinereading a file line by line using the read_line function, the function is called until feofread_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() getlineif 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);
}

feofUse the and functions in C ferrorto test the valid position of a file stream

Additionally, before we read the contents of a file, we can use feofto test the position of the file stream. In this case, we freadread the file using a call to read the file, which statretrieves the size of the file using the function call. Note that the buffer that stores the read bytes is mallocallocated on the heap using the function. Additionally, we include the function in a conditional statement ferror, EOFalong 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);
}

ferroris also part of the I/O library and can be FILEcalled 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 filenamethe contents of the file specified by the variable. In the previous sample code, we used printfthe function to output the stored contents, but a more error-prone method is fwriteto call , which can print a given number of bytes to FILEthe 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);
}

Previous: None

Next:Flushing stdout output stream in C

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.

Article URL:

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:178 Category:C语言

本文介绍了如何在 C 语言中使用位掩码。使用 struct 关键字在 C 语言中定义位掩码数据

C 语言中的排序函数

Publish Date:2023/05/07 Views:212 Category:C语言

本文演示了如何在 C 语言中使用标准库排序函数。使用 qsort 函数对 C 语言中的整数数组进行排序

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial