C++笔记之C语言中常见的文件操作函数汇总

C++笔记之C语言中常见的文件操作函数汇总

文章目录

  • C++笔记之C语言中常见的文件操作函数汇总
    • [1. `fopen()`: 打开文件并返回文件指针](#1. fopen(): 打开文件并返回文件指针)
    • [2. `fclose()`: 关闭文件](#2. fclose(): 关闭文件)
    • [3. `fread()`: 从文件中读取数据](#3. fread(): 从文件中读取数据)
    • [4. `fwrite()`: 向文件写入数据](#4. fwrite(): 向文件写入数据)
    • [5. `fgetc()`: 从文件中读取一个字符](#5. fgetc(): 从文件中读取一个字符)
    • [6. `fputc()`: 向文件写入一个字符](#6. fputc(): 向文件写入一个字符)
    • [7. `fgets()`: 从文件中读取一行文本](#7. fgets(): 从文件中读取一行文本)
    • [8. `fputs()`: 向文件写入一行文本](#8. fputs(): 向文件写入一行文本)
    • [9. `fprintf()`: 格式化写入数据到文件](#9. fprintf(): 格式化写入数据到文件)
    • [10. `fscanf()`: 从文件中格式化读取数据](#10. fscanf(): 从文件中格式化读取数据)
    • [11. `feof()`: 检查文件结束标志](#11. feof(): 检查文件结束标志)
    • [12. `ftell()`: 获取文件位置指针的当前位置](#12. ftell(): 获取文件位置指针的当前位置)
    • [13. `fseek()`: 设置文件位置指针的位置](#13. fseek(): 设置文件位置指针的位置)
    • [14. `rewind()`: 将文件位置指针重置到文件的开头](#14. rewind(): 将文件位置指针重置到文件的开头)
    • [15. `remove()`: 删除文件](#15. remove(): 删除文件)
    • [16. `rename()`: 重命名文件](#16. rename(): 重命名文件)
    • [17. `tmpfile()`: 创建临时文件](#17. tmpfile(): 创建临时文件)
    • [18. `tmpnam()`: 生成临时文件名](#18. tmpnam(): 生成临时文件名)
    • [19. `feof()`: 检查文件结束标志](#19. feof(): 检查文件结束标志)
    • [20. `fflush()`: 刷新文件缓冲区](#20. fflush(): 刷新文件缓冲区)
    • [21. `ferror()`: 检查文件错误标志](#21. ferror(): 检查文件错误标志)
    • [22. `perror()`: 打印错误消息](#22. perror(): 打印错误消息)
    • [23. `clearerr()`: 清除文件错误和结束标志](#23. clearerr(): 清除文件错误和结束标志)
    • [24. `setvbuf()`: 设置文件缓冲区](#24. setvbuf(): 设置文件缓冲区)
    • [25. `fileno()`: 获取文件描述符](#25. fileno(): 获取文件描述符)
    • [26. `flockfile()`: 锁定文件以进行线程安全的访问](#26. flockfile(): 锁定文件以进行线程安全的访问)
    • [27. `funlockfile()`: 解锁文件以允许其他线程访问](#27. funlockfile(): 解锁文件以允许其他线程访问)
    • [28. `getc()`: 从标准输入流中读取一个字符](#28. getc(): 从标准输入流中读取一个字符)
    • [29. `putc()`: 将字符写入标准输出流](#29. putc(): 将字符写入标准输出流)
    • [30. `getchar()`: 从标准输入流中读取一个字符](#30. getchar(): 从标准输入流中读取一个字符)
    • [31. `putchar()`: 将字符写入标准输出流](#31. putchar(): 将字符写入标准输出流)
    • [32. `feof()`: 检查文件结束标志](#32. feof(): 检查文件结束标志)
    • [33. `ferror()`: 检查文件错误标志](#33. ferror(): 检查文件错误标志)
    • [34. `clearerr()`: 清除文件错误和结束标志](#34. clearerr(): 清除文件错误和结束标志)
    • 35.一些易混淆的函数以及它们的详细说明
    • 36.每个函数的参数列表参数数量说明

1. fopen(): 打开文件并返回文件指针

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

int main() {
    FILE *file = fopen("example.txt", "w"); // 以写入模式打开文件
    if (file == NULL) {
        perror("文件打开失败");
        return 1;
    }
    
    // 使用文件指针进行写入操作
    
    fclose(file); // 关闭文件
    return 0;
}

2. fclose(): 关闭文件

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

int main() {
    FILE *file = fopen("example.txt", "w");
    if (file == NULL) {
        perror("文件打开失败");
        return 1;
    }
    
    // 使用文件指针进行写入操作
    
    fclose(file); // 关闭文件
    return 0;
}

3. fread(): 从文件中读取数据

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

int main() {
    FILE *file = fopen("data.bin", "rb"); // 以二进制模式打开文件以供读取
    if (file == NULL) {
        perror("文件打开失败");
        return 1;
    }
    
    char buffer[100];
    size_t bytesRead = fread(buffer, sizeof(char), sizeof(buffer), file);
    
    // 处理读取的数据
    
    fclose(file);
    return 0;
}

4. fwrite(): 向文件写入数据

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

int main() {
    FILE *file = fopen("output.txt", "w"); // 以写入模式打开文件
    if (file == NULL) {
        perror("文件打开失败");
        return 1;
    }
    
    char data[] = "Hello, World!";
    size_t bytesWritten = fwrite(data, sizeof(char), sizeof(data), file);
    
    // 处理写入的数据
    
    fclose(file);
    return 0;
}

5. fgetc(): 从文件中读取一个字符

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

int main() {
    FILE *file = fopen("input.txt", "r");
    if (file == NULL) {
        perror("文件打开失败");
        return 1;
    }
    
    int ch = fgetc(file);
    if (ch != EOF) {
        // 处理读取到的字符
    }
    
    fclose(file);
    return 0;
}

6. fputc(): 向文件写入一个字符

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

int main() {
    FILE *file = fopen("output.txt", "w");
    if (file == NULL) {
        perror("文件打开失败");
        return 1;
    }
    
    int ch = 'A';
    int result = fputc(ch, file);
    if (result == ch) {
        // 写入成功
    }
    
    fclose(file);
    return 0;
}

7. fgets(): 从文件中读取一行文本

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

int main() {
    FILE *file = fopen("input.txt", "r");
    if (file == NULL) {
        perror("文件打开失败");
        return 1;
    }
    
    char buffer[100];
    if (fgets(buffer, sizeof(buffer), file) != NULL) {
        // 处理读取到的文本行
    }
    
    fclose(file);
    return 0;
}

8. fputs(): 向文件写入一行文本

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

int main() {
    FILE *file = fopen("output.txt", "w");
    if (file == NULL) {
        perror("文件打开失败");
        return 1;
    }
    
    const char *text = "Hello, World!";
    if (fputs(text, file) != EOF) {
        // 写入成功
    }
    
    fclose(file);
    return 0;
}

9. fprintf(): 格式化写入数据到文件

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

int main() {
    FILE *file = fopen("output.txt", "w");
    if (file == NULL) {
        perror("文件打开失败");
        return 1;
    }
    
    int number = 42;
    fprintf(file, "The answer is: %d\n", number);
    
    fclose(file);
    return 0;
}

10. fscanf(): 从文件中格式化读取数据

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

int main() {
    FILE *file = fopen("input.txt", "r");
    if (file == NULL) {
        perror("文件打开失败");
        return 1;
    }
    
    int number;
    if (fscanf(file, "%d", &number) == 1) {
        // 处理读取到的整数
    }
    
    fclose(file);
    return 0;
}

11. feof(): 检查文件结束标志

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

int main() {
    FILE *file = fopen("input.txt", "r");
    if (file == NULL) {
        perror("文件打开失败");
        return 1;
    }
    
    while (!feof(file)) {
        int ch = fgetc(file);
        if (ch != EOF) {
            // 处理读取到的字符
        }
    }
    
    fclose(file);
    return 0;
}

12. ftell(): 获取文件位置指针的当前位置

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

int main() {
    FILE *file = fopen("data.bin", "rb");
    if (file == NULL) {
        perror("文件打开失败");
        return 1;
    }
    
    fseek(file, 0, SEEK_END); // 移动文件指针到文件末尾
    long fileSize = ftell(file); // 获取当前位置
    
    // 处理文件大小
    
    fclose(file);
    return 0;
}

13. fseek(): 设置文件位置指针的位置

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

int main() {
    FILE *file = fopen("data.bin", "rb");
    if (file == NULL) {
        perror("文件打开失败");
        return 1;
    }
    
    fseek(file, 50, SEEK_SET); // 移动文件指针到文件的第 50 个字节位置
    
    // 处理文件指针的位置
    
    fclose(file);
    return 0;
}

14. rewind(): 将文件位置指针重置到文件的开头

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

int main() {
    FILE *file = fopen("data.bin", "rb");
    if (file == NULL) {


        perror("文件打开失败");
        return 1;
    }
    
    rewind(file); // 重置文件指针到文件开头
    
    // 处理文件内容
    
    fclose(file);
    return 0;
}

15. remove(): 删除文件

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

int main() {
    if (remove("example.txt") == 0) {
        // 文件删除成功
    } else {
        perror("文件删除失败");
    }
    
    return 0;
}

16. rename(): 重命名文件

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

int main() {
    if (rename("old_name.txt", "new_name.txt") == 0) {
        // 文件重命名成功
    } else {
        perror("文件重命名失败");
    }
    
    return 0;
}

17. tmpfile(): 创建临时文件

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

int main() {
    FILE *tempFile = tmpfile(); // 创建临时文件
    if (tempFile != NULL) {
        // 使用临时文件进行操作
    } else {
        perror("创建临时文件失败");
    }
    
    return 0;
}

18. tmpnam(): 生成临时文件名

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

int main() {
    char *tempFileName = tmpnam(NULL); // 生成临时文件名
    if (tempFileName != NULL) {
        // 使用临时文件名
    } else {
        perror("生成临时文件名失败");
    }
    
    return 0;
}

19. feof(): 检查文件结束标志

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

int main() {
    FILE *file = fopen("input.txt", "r");
    if (file == NULL) {
        perror("文件打开失败");
        return 1;
    }
    
    while (!feof(file)) {
        int ch = fgetc(file);
        if (ch != EOF) {
            // 处理读取到的字符
        }
    }
    
    fclose(file);
    return 0;
}

20. fflush(): 刷新文件缓冲区

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

int main() {
    FILE *file = fopen("output.txt", "w");
    if (file == NULL) {
        perror("文件打开失败");
        return 1;
    }
    
    fputs("Hello, World!", file);
    fflush(file); // 刷新文件缓冲区,确保数据被写入磁盘
    
    fclose(file);
    return 0;
}

21. ferror(): 检查文件错误标志

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

int main() {
    FILE *file = fopen("example.txt", "r");
    if (file == NULL) {
        perror("文件打开失败");
        return 1;
    }
    
    int ch = fgetc(file);
    if (ch == EOF) {
        if (ferror(file)) {
            perror("读取文件时发生错误");
        }
    }
    
    fclose(file);
    return 0;
}

22. perror(): 打印错误消息

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

int main() {
    FILE *file = fopen("example.txt", "r");
    if (file == NULL) {
        perror("文件打开失败");
        return 1;
    }
    
    // 尝试执行文件操作
    
    fclose(file);
    return 0;
}

23. clearerr(): 清除文件错误和结束标志

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

int main() {
    FILE *file = fopen("example.txt", "r");
    if (file == NULL) {
        perror("文件打开失败");
        return 1;
    }
    
    clearerr(file); // 清除错误和结束标志
    
    // 尝试执行文件操作
    
    fclose(file);
    return 0;
}

24. setvbuf(): 设置文件缓冲区

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

int main() {
    FILE *file = fopen("example.txt", "w");
    if (file == NULL) {
        perror("文件打开失败");
        return 1;
    }
    
    char buffer[1024];
    setvbuf(file, buffer, _IOFBF, sizeof(buffer)); // 设置文件缓冲区
    
    // 使用文件进行写入操作
    
    fclose(file);
    return 0;
}

25. fileno(): 获取文件描述符

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

int main() {
    FILE *file = fopen("example.txt", "r");
    if (file == NULL) {
        perror("文件打开失败");
        return 1;
    }
    
    int fd = fileno(file); // 获取文件描述符
    
    // 使用文件描述符进行操作
    
    fclose(file);
    return 0;
}

26. flockfile(): 锁定文件以进行线程安全的访问

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

void *thread_function(void *arg) {
    FILE *file = (FILE *)arg;
    
    flockfile(file); // 锁定文件
    
    // 使用文件进行操作
    
    funlockfile(file); // 解锁文件
    return NULL;
}

int main() {
    FILE *file = fopen("example.txt", "w");
    if (file == NULL) {
        perror("文件打开失败");
        return 1;
    }
    
    pthread_t thread;
    pthread_create(&thread, NULL, thread_function, file);
    
    // 主线程也可以使用文件进行操作
    
    pthread_join(thread, NULL);
    
    fclose(file);
    return 0;
}

27. funlockfile(): 解锁文件以允许其他线程访问

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

void *thread_function(void *arg) {
    FILE *file = (FILE *)arg;
    
    flockfile(file); // 锁定文件
    
    // 使用文件进行操作
    
    funlockfile(file); // 解锁文件
    return NULL;
}

int main() {
    FILE *file = fopen("example.txt", "w");
    if (file == NULL) {
        perror("文件打开失败");
        return 1;
    }
    
    pthread_t thread;
    pthread_create(&thread, NULL, thread_function, file);
    
    // 主线程也可以使用文件进行操作
    
    pthread_join(thread, NULL);
    
    fclose(file);
    return 0;
}

28. getc(): 从标准输入流中读取一个字符

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

int main() {
    int ch = getc(stdin); // 从标准输入流读取一个字符
    if (ch != EOF) {


        // 处理读取到的字符
    }
    
    return 0;
}

29. putc(): 将字符写入标准输出流

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

int main() {
    int ch = 'A';
    int result = putc(ch, stdout); // 将字符写入标准输出流
    if (result == ch) {
        // 写入成功
    }
    
    return 0;
}

30. getchar(): 从标准输入流中读取一个字符

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

int main() {
    int ch = getchar(); // 从标准输入流读取一个字符
    if (ch != EOF) {
        // 处理读取到的字符
    }
    
    return 0;
}

31. putchar(): 将字符写入标准输出流

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

int main() {
    int ch = 'A';
    int result = putchar(ch); // 将字符写入标准输出流
    if (result == ch) {
        // 写入成功
    }
    
    return 0;
}

32. feof(): 检查文件结束标志

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

int main() {
    while (!feof(stdin)) {
        int ch = getc(stdin);
        if (ch != EOF) {
            // 处理读取到的字符
        }
    }
    
    return 0;
}

33. ferror(): 检查文件错误标志

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

int main() {
    int ch = getc(stdin);
    if (ch == EOF) {
        if (ferror(stdin)) {
            perror("读取标准输入流时发生错误");
        }
    }
    
    return 0;
}

34. clearerr(): 清除文件错误和结束标志

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

int main() {
    int ch = getc(stdin);
    if (ch == EOF) {
        clearerr(stdin); // 清除错误和结束标志
    }
    
    return 0;
}

35.一些易混淆的函数以及它们的详细说明

  1. fread()fwrite(): 这两个函数用于二进制文件的读取和写入。fread() 用于从文件中读取二进制数据,而 fwrite() 用于将二进制数据写入文件。它们不适用于文本文件。

  2. fgetc()fputc(): 这两个函数用于字符级别的读取和写入。fgetc() 从文件中读取一个字符,而 fputc() 将一个字符写入文件。它们适用于文本文件。

  3. fgets()fputs(): 这两个函数用于文本行的读取和写入。fgets() 从文件中读取一行文本,而 fputs() 将一行文本写入文件。它们适用于文本文件。

  4. getc()putc(): 这两个函数类似于 fgetc()fputc(),但它们操作标准输入和标准输出流,而不是文件。getc(stdin) 从标准输入中读取一个字符,而 putc(ch, stdout) 将字符写入标准输出。

  5. getchar()putchar(): 这两个函数也操作标准输入和标准输出流,类似于 getc()putc(),但它们不需要传递流参数。getchar() 从标准输入中读取一个字符,而 putchar() 将字符写入标准输出。

  6. feof()ferror(): 这两个函数都用于检查文件操作的状态,但它们检查的是不同的条件。feof() 检查文件结束标志,如果文件已到达末尾,它返回非零值。ferror() 检查文件错误标志,如果发生了文件相关的错误,它返回非零值。通常,你会在文件操作之后使用这两个函数来检查状态。

36.每个函数的参数列表参数数量说明

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

int main() {
    // fopen(): 2个参数
    FILE *file = fopen("example.txt", "w"); // 文件名和文件打开模式

    // fclose(): 1个参数
    fclose(file); // 文件指针

    // fread(): 4个参数
    char buffer[100];
    size_t bytesRead = fread(buffer, sizeof(char), sizeof(buffer), file); // 数据缓冲区,每个数据项的大小,数据项的数量,文件指针

    // fwrite(): 4个参数
    char data[] = "Hello, World!";
    size_t bytesWritten = fwrite(data, sizeof(char), sizeof(data), file); // 数据缓冲区,每个数据项的大小,数据项的数量,文件指针

    // fgetc(): 1个参数
    int ch = fgetc(file); // 文件指针

    // fputc(): 2个参数
    int ch = 'A';
    int result = fputc(ch, file); // 字符,文件指针

    // fgets(): 3个参数
    char buffer[100];
    if (fgets(buffer, sizeof(buffer), file) != NULL) { // 字符串缓冲区,最大读取字符数,文件指针
        // 处理读取到的文本行
    }

    // fputs(): 2个参数
    const char *text = "Hello, World!";
    if (fputs(text, file) != EOF) { // 字符串,文件指针
        // 写入成功
    }

    // fprintf(): 2个参数
    int number = 42;
    fprintf(file, "The answer is: %d\n", number); // 文件指针,格式化字符串和数据

    // fscanf(): 2个参数
    int number;
    if (fscanf(file, "%d", &number) == 1) { // 文件指针,格式化字符串和数据
        // 处理读取到的整数
    }

    // feof(): 1个参数
    while (!feof(file)) { // 文件指针
        int ch = fgetc(file);
        if (ch != EOF) {
            // 处理读取到的字符
        }
    }

    // ftell(): 1个参数
    long currentPosition = ftell(file); // 文件指针

    // fseek(): 3个参数
    fseek(file, 50, SEEK_SET); // 文件指针,偏移量,起始位置

    // rewind(): 1个参数
    rewind(file); // 文件指针

    // remove(): 1个参数
    if (remove("example.txt") == 0) { // 文件名
        // 文件删除成功
    }

    // rename(): 2个参数
    if (rename("old_name.txt", "new_name.txt") == 0) { // 旧文件名,新文件名
        // 文件重命名成功
    }

    // tmpfile(): 无参数
    FILE *tempFile = tmpfile(); // 无参数

    // tmpnam(): 1个参数
    char *tempFileName = tmpnam(NULL); // 字符串缓冲区(可选)

    // feof(): 1个参数
    while (!feof(file)) { // 文件指针
        int ch = fgetc(file);
        if (ch != EOF) {
            // 处理读取到的字符
        }
    }

    // fflush(): 1个参数
    fflush(file); // 文件指针

    // ferror(): 1个参数
    int ch = fgetc(file);
    if (ch == EOF) {
        if (ferror(file)) { // 文件指针
            perror("读取文件时发生错误");
        }
    }

    // perror(): 1个参数
    FILE *file = fopen("example.txt", "r");
    if (file == NULL) {
        perror("文件打开失败"); // 错误消息前缀字符串
        return 1;
    }

    // clearerr(): 1个参数
    int ch = fgetc(file);
    if (ch == EOF) {
        clearerr(file); // 文件指针
    }

    // setvbuf(): 3个参数
    char buffer[1024];
    setvbuf(file, buffer, _IOFBF, sizeof(buffer)); // 文件指针,缓冲区,缓冲模式

    // fileno(): 1个参数
    int fileDescriptor = fileno(file); // 文件指针

    // flockfile(): 1个参数
    flockfile(file); // 文件指针

    // funlockfile(): 1个参数
    funlockfile(file); // 文件指针

    // getc(): 1个参数
    int ch = getc(stdin); // 文件指针

    // putc(): 2个参数
    int ch = 'A';
    int result = putc(ch, stdout); // 字符,文件指针

    // getchar(): 无参数
    int ch = getchar(); // 无参数

    // putchar(): 1个参数
    int ch = 'A';
    int result = putchar(ch); // 字符

    // feof(): 1个参数
    while (!feof(stdin)) { // 文件指针
        int ch = getc(stdin);
        if (ch != EOF) {
            // 处理读取到的字符
        }
    }

    // ferror(): 1个参数
    int ch = getc(stdin);
    if (ch == EOF) {
        if (ferror(stdin)) { // 文件指针
            perror("读取标准输入流时发生错误");
        }
    }

    // clearerr(): 1个参数
    int ch = getc(stdin);
    if (ch == EOF) {
        clearerr(stdin); // 文件指针
    }

    return 0;
}
相关推荐
我要吐泡泡了哦15 分钟前
GAMES104:15 游戏引擎的玩法系统基础-学习笔记
笔记·学习·游戏引擎
骑鱼过海的猫12315 分钟前
【tomcat】tomcat学习笔记
笔记·学习·tomcat
贾saisai2 小时前
Xilinx系FPGA学习笔记(九)DDR3学习
笔记·学习·fpga开发
北岛寒沫2 小时前
JavaScript(JS)学习笔记 1(简单介绍 注释和输入输出语句 变量 数据类型 运算符 流程控制 数组)
javascript·笔记·学习
烟雨666_java2 小时前
JDBC笔记
笔记
2401_858286112 小时前
52.【C语言】 字符函数和字符串函数(strcat函数)
c语言·开发语言
GEEKVIP2 小时前
Android 恢复挑战和解决方案:如何从 Android 设备恢复删除的文件
android·笔记·安全·macos·智能手机·电脑·笔记本电脑
jiao000014 小时前
数据结构——队列
c语言·数据结构·算法
铁匠匠匠4 小时前
从零开始学数据结构系列之第六章《排序简介》
c语言·数据结构·经验分享·笔记·学习·开源·课程设计
C-SDN花园GGbond4 小时前
【探索数据结构与算法】插入排序:原理、实现与分析(图文详解)
c语言·开发语言·数据结构·排序算法