【C语言】文件操作函数详解

目录

  • C语言文件操作函数详解
    • 表格汇总
    • [1. `fopen`](#1. fopen)
    • [2. `fclose`](#2. fclose)
    • [3. `fread`](#3. fread)
    • [4. `fwrite`](#4. fwrite)
    • [5. `fseek`](#5. fseek)
    • [6. `ftell`](#6. ftell)
    • [7. `rewind`](#7. rewind)
    • [8. `fprintf`](#8. fprintf)
    • [9. `fscanf`](#9. fscanf)
    • [10. `feof`](#10. feof)
    • [11. `ferror`](#11. ferror)
    • [12. `clearerr`](#12. clearerr)
    • [13. 总结](#13. 总结)
    • [14. 附录:函数参考表](#14. 附录:函数参考表)
    • [15. 结束语](#15. 结束语)
    • 相关文章:

C语言文件操作函数详解

C语言提供了一组标准库函数来处理文件操作,这些函数定义在 <stdio.h> 头文件中。文件操作包括文件的打开、读写、关闭以及文件属性的查询等。以下是常用文件操作函数的详细讲解,包括函数原型、参数说明、返回值说明、示例代码和表格汇总。

表格汇总

函数 作用 示例 输出
fopen 打开文件 fopen("file.txt", "r") 文件指针
fclose 关闭文件 fclose(file) 返回 0 或 EOF
fread 从文件中读取数据 fread(buffer, size, count, file) 读取的数据
fwrite 将数据写入文件 fwrite(buffer, size, count, file) 写入的数据
fseek 设置文件位置指针 fseek(file, offset, origin) 返回 0 或 EOF
ftell 获取文件位置指针 ftell(file) 文件位置
rewind 重置文件位置指针到文件开头 rewind(file) 无返回值
fprintf 格式化输出到文件 fprintf(file, "data: %d", value) 格式化的字符串
fscanf 从文件中格式化输入 fscanf(file, "%d", &value) 读取的值
feof 检查文件是否到达文件末尾 feof(file) 返回非零值或 0
ferror 检查文件操作是否发生错误 ferror(file) 错误代码
clearerr 清除文件流的错误和EOF标志 clearerr(file) 无返回值

1. fopen

函数原型:

c 复制代码
FILE *fopen(const char *filename, const char *mode);

参数说明:

  • filename: 要打开的文件的路径。
  • mode: 文件打开模式,如 "r"(只读)、"w"(只写)、"a"(追加)等。

返回值说明:

  • 成功时,返回指向 FILE 类型的文件指针;失败时,返回 NULL。

示例代码:

c 复制代码
#include <stdio.h>

int main() {
    FILE *file;

    file = fopen("example.txt", "w");
    if (file == NULL) {
        perror("无法打开文件");
        return 1;
    }

    fprintf(file, "Hello, World!\n");
    fclose(file);

    return 0;
}

表格说明:

函数 作用 示例 输出
fopen 打开文件 fopen("example.txt", "w") 文件指针或 NULL

2. fclose

函数原型:

c 复制代码
int fclose(FILE *stream);

参数说明:

  • stream: 要关闭的文件指针。

返回值说明:

  • 成功时,返回 0;失败时,返回 EOF。

示例代码:

c 复制代码
#include <stdio.h>

int main() {
    FILE *file;

    file = fopen("example.txt", "w");
    if (file == NULL) {
        perror("无法打开文件");
        return 1;
    }

    fprintf(file, "Hello, World!\n");
    if (fclose(file) != 0) {
        perror("无法关闭文件");
        return 1;
    }

    return 0;
}

表格说明:

函数 作用 示例 输出
fclose 关闭文件 fclose(file) 0 或 EOF

3. fread

函数原型:

c 复制代码
size_t fread(void *ptr, size_t size, size_t count, FILE *stream);

参数说明:

  • ptr: 指向存储读取数据的内存块的指针。
  • size: 每个元素的字节数。
  • count: 要读取的元素数量。
  • stream: 文件指针。

返回值说明:

  • 返回成功读取的元素数量。如果返回值小于 count,可能发生了文件结束或读取错误。

示例代码:

c 复制代码
#include <stdio.h>

int main() {
    FILE *file;
    char buffer[20];
    size_t bytesRead;

    file = fopen("example.txt", "r");
    if (file == NULL) {
        perror("无法打开文件");
        return 1;
    }

    bytesRead = fread(buffer, sizeof(char), sizeof(buffer) - 1, file);
    buffer[bytesRead] = '\0'; // 确保字符串结束符
    printf("读取数据: %s\n", buffer);

    fclose(file);

    return 0;
}

表格说明:

函数 作用 示例 输出
fread 从文件中读取数据 fread(buffer, size, count, file) 读取的数据

4. fwrite

函数原型:

c 复制代码
size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream);

参数说明:

  • ptr: 指向要写入文件的内存块的指针。
  • size: 每个元素的字节数。
  • count: 要写入的元素数量。
  • stream: 文件指针。

返回值说明:

  • 返回成功写入的元素数量。如果返回值小于 count,可能发生了写入错误。

示例代码:

c 复制代码
#include <stdio.h>

int main() {
    FILE *file;
    const char *text = "Hello, File Writing!";

    file = fopen("example.txt", "w");
    if (file == NULL) {
        perror("无法打开文件");
        return 1;
    }

    if (fwrite(text, sizeof(char), strlen(text), file) < strlen(text)) {
        perror("写入文件失败");
        fclose(file);
        return 1;
    }

    fclose(file);

    return 0;
}

表格说明:

函数 作用 示例 输出
fwrite 将数据写入文件 fwrite(buffer, size, count, file) 写入的数据

5. fseek

函数原型:

c 复制代码
int fseek(FILE *stream, long offset, int whence);

参数说明:

  • stream: 文件指针。
  • offset: 从 whence 指定的位置偏移的字节数。
  • whence: 指定偏移的起始位置,可以是 SEEK_SET(文件开头)、SEEK_CUR(当前位置)、SEEK_END(文件末尾)。

返回值说明:

  • 成功时返回 0;失败时返回 -1。

示例代码:

c 复制代码
#include <stdio.h>

int main() {
    FILE *file;
    long position;

    file = fopen("example.txt", "r");
    if (file == NULL) {
        perror("无法打开文件");
        return 1;
    }

    fseek(file, 5, SEEK_SET);  // 从文件开头偏移5个字节
    position = ftell(file);    // 获取当前文件位置
    printf("文件位置: %ld\n", position); // 输出: 文件位置: 5

    fclose(file);

    return 0;
}

表格说明:

函数 作用 示例 输出
fseek 设置文件位置指针 fseek(file, offset, whence) 成功时 0 或 -1

6. ftell

函数原型:

c 复制代码
long ftell(FILE *stream);

参数说明:

  • stream: 文件指针,指向一个已打开的文件。

返回值说明:

  • 返回从文件开头到当前位置的字节数。成功时返回当前文件位置的偏移量,失败时返回 -1L。

示例代码:

c 复制代码
#include <stdio.h>

int main() {
    FILE *file;
    long position;

    file = fopen("example.txt", "r");
    if (file == NULL) {
        perror("无法打开文件");
        return 1;
    }

    fseek(file, 10, SEEK_SET); // 从文件开头偏移10个字节
    position = ftell(file);    // 获取当前文件位置
    printf("文件当前位置: %ld\n", position); // 输出: 文件当前位置: 10

    fclose(file);

    return 0;
}

表格说明:

函数 作用 示例 输出
ftell 获取文件当前位置 ftell(file) 文件当前位置: 10

7. rewind

函数原型:

c 复制代码
void rewind(FILE *stream);

参数说明:

  • stream: 文件指针,指向一个已打开的文件。

返回值说明:

  • 无返回值。此函数将文件位置指针重置到文件的开头。

示例代码:

c 复制代码
#include <stdio.h>

int main() {
    FILE *file;
    char buffer[50];

    file = fopen("example.txt", "r");
    if (file == NULL) {
        perror("无法打开文件");
        return 1;
    }

    fread(buffer, sizeof(char), sizeof(buffer) - 1, file); // 读取文件数据
    buffer[sizeof(buffer) - 1] = '\0'; // 确保字符串结束符

    rewind(file); // 重置文件指针到开头
    fread(buffer, sizeof(char), sizeof(buffer) - 1, file); // 重新读取文件数据
    buffer[sizeof(buffer) - 1] = '\0'; // 确保字符串结束符

    printf("重新读取的数据: %s\n", buffer); // 输出重新读取的数据

    fclose(file);

    return 0;
}

表格说明:

函数 作用 示例 输出
rewind 重置文件指针到文件开头 rewind(file) 无输出(重置指针)

8. fprintf

函数原型:

c 复制代码
int fprintf(FILE *stream, const char *format, ...);

参数说明:

  • stream: 文件指针,指向一个已打开的文件。
  • format: 格式字符串,用于指定输出格式。
  • ...: 其他格式化数据,根据 format 中的占位符提供。

返回值说明:

  • 成功时,返回输出的字符总数;失败时返回 EOF。

示例代码:

c 复制代码
#include <stdio.h>

int main() {
    FILE *file;
    int number = 42;

    file = fopen("example.txt", "w");
    if (file == NULL) {
        perror("无法打开文件");
        return 1;
    }

    if (fprintf(file, "The answer is %d\n", number) < 0) {
        perror("写入文件失败");
        fclose(file);
        return 1;
    }

    fclose(file);

    return 0;
}

表格说明:

函数 作用 示例 输出
fprintf 格式化输出到文件 fprintf(file, "data: %d", value) 格式化的字符串

9. fscanf

函数原型:

c 复制代码
int fscanf(FILE *stream, const char *format, ...);

参数说明:

  • stream: 文件指针,指向一个已打开的文件。
  • format: 格式字符串,用于指定输入格式。
  • ...: 其他格式化输入,根据 format 中的占位符提供。

返回值说明:

  • 成功时,返回成功匹配和赋值的项目数;失败时返回 EOF。

示例代码:

c 复制代码
#include <stdio.h>

int main() {
    FILE *file;
    int number;

    file = fopen("example.txt", "r");
    if (file == NULL) {
        perror("无法打开文件");
        return 1;
    }

    if (fscanf(file, "The answer is %d", &number) != 1) {
        perror("读取文件失败");
        fclose(file);
        return 1;
    }

    printf("读取的数字: %d\n", number);

    fclose(file);

    return 0;
}

表格说明:

函数 作用 示例 输出
fscanf 从文件中格式化输入 fscanf(file, "%d", &value) 读取的值

10. feof

函数原型:

c 复制代码
int feof(FILE *stream);

参数说明:

  • stream: 文件指针,指向一个已打开的文件。

返回值说明:

  • 如果到达文件末尾,返回非零值;否则返回 0。

示例代码:

c 复制代码
#include <stdio.h>

int main() {
    FILE *file;
    char buffer[20];

    file = fopen("example.txt", "r");
    if (file == NULL) {
        perror("无法打开文件");
        return 1;
    }

    while (fread(buffer, sizeof(char), sizeof(buffer) - 1, file) > 0) {
        buffer[sizeof(buffer) - 1] = '\0';
        printf("读取的数据: %s\n", buffer);
    }

    if (feof(file)) {
        printf("到达文件末尾\n");
    } else {
        printf("文件读取错误\n");
    }

    fclose(file);

    return 0;
}

表格说明:

函数 作用 示例 输出
feof 检查文件是否到达文件末尾 feof(file) 到达文件末尾

11. ferror

函数原型:

c 复制代码
int ferror(FILE *stream);

参数说明:

  • stream: 文件指针,指向一个已打开的文件。

返回值说明:

  • 返回非零值表示发生了错误;返回 0 表示没有错误。

示例代码:

c 复制代码
#include <stdio.h>

int main() {
    FILE *file;
    char buffer[20];

    file = fopen("example.txt", "r");
    if (file == NULL) {
        perror("无法打开文件");
        return 1;
    }

    if (fread(buffer, sizeof(char), sizeof(buffer) - 1, file) < 0) {
        if (ferror(file)) {
            perror("读取文件错误");
        }
        fclose(file);
        return 1;
    }

    fclose(file);

    return 0;
}

表格说明:

函数 作用 示例 输出
ferror 检查文件操作是否发生错误 ferror(file) 错误代码

12. clearerr

函数原型:

c 复制代码
void clearerr(FILE *stream);

参数说明:

  • stream: 文件指针,指向一个已打开的文件。

返回值说明:

  • 无返回值。此函数清除流的错误标志和EOF标志。

示例代码:

c 复制代码
#include <stdio.h>

int main() {
    FILE *file;
    char buffer[10];

    file = fopen("example.txt", "r");
    if (file == NULL) {
        perror("无法打开文件");
        return 1;
    }

    fread(buffer, sizeof(char), sizeof(buffer) - 1, file); // 模拟读取
    if (ferror(file)) {
        printf("读取文件时发生错误\n");
        clearerr(file); // 清除错误标志
    }

    // 继续操作文件

    fclose(file);

    return 0;
}

表格说明:

函数 作用 示例 输出
clearerr 清除文件流的错误和EOF标志 clearerr(file) 无输出(清除标志)

13. 总结

C语言的文件操作函数提供了一系列用于处理文件的工具,包括打开、关闭、读写、定位文件指针以及检查文件状态等。这些函数为程序员提供了灵活的文件管理能力,从简单的文件读写到复杂的文件处理任务,都可以通过这些标准库函数来实现。掌握这些函数的使用对于编写健壮且可靠的C程序至关重要。

14. 附录:函数参考表

函数 作用 示例 返回值
fopen 打开文件 fopen("file.txt", "r") 文件指针或 NULL
fclose 关闭文件 fclose(file) 0 或 EOF
fread 从文件中读取数据 fread(buffer, size, count, file) 读取的数据
fwrite 将数据写入文件 fwrite(buffer, size, count, file) 写入的数据
fseek 设置文件位置指针 fseek(file, offset, SEEK_SET) 0 或 EOF
ftell 获取文件当前位置 ftell(file) 文件位置
rewind 重置文件位置指针到文件开头 rewind(file) 无返回值
fprintf 格式化输出到文件 fprintf(file, "data: %d", value) 格式化的字符串
fscanf 从文件中格式化输入 fscanf(file, "%d", &value) 读取的值
feof 检查文件是否到达文件末尾 feof(file) 非零值或 0
ferror 检查文件操作是否发生错误 ferror(file) 错误代码
clearerr 清除文件流的错误和EOF标志 clearerr(file) 无返回值

通过理解和运用这些文件操作函数,可以有效地进行文件读写操作,并处理各种文件相关的错误和状态检查。这对于实现文件管理功能以及确保程序的健壮性具有重要意义。

15. 结束语

  1. 本节内容已经全部介绍完毕,希望通过这篇文章,大家对C语言文件操作函数有了更深入的理解和认识。
  2. 感谢各位的阅读和支持,如果觉得这篇文章对你有帮助,请不要吝惜你的点赞和评论 ,这对我们非常重要。再次感谢大家的关注和支持 !点我关注❤️

相关文章:

相关推荐
Wang's Blog2 分钟前
Java 项目实战: 外卖平台优化-YApi接口管理平台与文档导入导出
java·开发语言·yapi
jimy18 分钟前
基类没有虚析构函数,基类指针删除派生类对象,行为是未定义的
开发语言·c++
迅猛龙办公室18 分钟前
python获取星期字符串
开发语言·python
xlq2232239 分钟前
全面复习2
c++
无名猿41 分钟前
移动构造与移动赋值:把资源偷过来
c++·性能优化·内存管理·现代c++·语法基础
老花眼猫1 小时前
数学艺术图案画-曼陀罗(93)
c语言·经验分享·青少年编程·课程设计
泡海椒1 小时前
JQuick-Excel dateFormat 转换实战:日期值与 FORMAT 显示格式的边界
开发语言·python·excel
水饺编程1 小时前
第1章:下载 Linux 0.12 内核
linux·c语言·汇编·ubuntu
All for pursuit.1 小时前
【回溯-7】494.目标和
数据结构·c++·算法·leetcode
白杨尚青1 小时前
C++入门篇(十三):vector(上)——动态数组:构造、空间增长与迭代器失效
开发语言·c++·笔记·算法·stl