C 语言文件操作
                           
天天向上
发布: 2025-04-01 23:49:37

原创
394 人浏览过

在 C 语言中,文件操作(包括文件的读取和写入)是通过标准库 stdio.h 中的函数来实现的。文件操作可以让程序在磁盘上存储数据,或者从磁盘上读取数据。常见的文件操作函数包括:fopen()fclose()fread()fwrite()fgets()fputs()fscanf()fprintf()

1. 文件打开:fopen()

在执行任何文件操作之前,必须首先打开文件。fopen() 函数用于打开一个文件并返回文件指针。

FILE *fopen(const char *filename, const char *mode);
  • filename:要打开的文件名。
  • mode:打开文件的模式,决定了文件的访问权限。

常用文件打开模式:

  • "r":只读模式,文件必须存在。
  • "w":写模式,文件不存在则创建,文件存在则清空文件内容。
  • "a":附加模式,文件不存在则创建,文件存在则从文件末尾开始写入。
  • "rb", "wb":以二进制模式读取或写入文件。
  • "r+":读写模式,文件必须存在。
  • "w+":读写模式,文件不存在则创建,文件存在则清空文件内容。
  • "a+":读写模式,文件不存在则创建,文件存在则从文件末尾开始读写。

2. 文件关闭:fclose()

打开文件后,应该在文件操作完成后关闭文件。使用 fclose() 函数关闭文件,释放资源。

int fclose(FILE *stream);
  • stream:文件指针。

3. 读取文件:fgetc()fgets()

文件读取有多种方法,fgetc()fgets() 是常用的读取字符或字符串的函数。

3.1 fgetc()

fgetc() 从文件中读取一个字符并返回。如果到达文件末尾或发生错误,则返回 EOF(文件结束标志)。

int fgetc(FILE *stream);

示例:

#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "r");
    char ch;

    if (file == NULL) {
        printf("Error opening file!\n");
        return 1;
    }

    while ((ch = fgetc(file)) != EOF) {
        putchar(ch);  // 输出文件中的字符
    }

    fclose(file);
    return 0;
}

3.2 fgets()

fgets() 用于从文件中读取一行。它会读取包括空格在内的整行内容,直到遇到换行符或文件末尾为止。

char *fgets(char *str, int n, FILE *stream);
  • str:存储读取数据的缓冲区。
  • n:要读取的最大字符数(包括 \0)。
  • stream:文件指针。

示例:

#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "r");
    char line[256];

    if (file == NULL) {
        printf("Error opening file!\n");
        return 1;
    }

    while (fgets(line, sizeof(line), file)) {
        printf("%s", line);  // 输出文件中的每一行
    }

    fclose(file);
    return 0;
}

4. 写入文件:fputc()fputs()

fputc()fputs() 用于向文件中写入字符或字符串。

4.1 fputc()

fputc() 向文件写入一个字符。

int fputc(int char, FILE *stream);

示例:

#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "w");

    if (file == NULL) {
        printf("Error opening file!\n");
        return 1;
    }

    fputc('A', file);  // 写入一个字符到文件

    fclose(file);
    return 0;
}

4.2 fputs()

fputs() 向文件写入一个字符串。

int fputs(const char *str, FILE *stream);

示例:

#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "w");

    if (file == NULL) {
        printf("Error opening file!\n");
        return 1;
    }

    fputs("Hello, World!", file);  // 写入字符串到文件

    fclose(file);
    return 0;
}

5. 格式化输入输出:fprintf()fscanf()

fprintf()fscanf() 用于以格式化的方式读写文件内容,类似于 printf()scanf()

5.1 fprintf()

fprintf() 向文件中写入格式化数据。

int fprintf(FILE *stream, const char *format, ...);

示例:

#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "w");

    if (file == NULL) {
        printf("Error opening file!\n");
        return 1;
    }

    int num = 42;
    fprintf(file, "The answer is: %d\n", num);  // 格式化输出到文件

    fclose(file);
    return 0;
}

5.2 fscanf()

fscanf() 从文件中读取格式化数据。

int fscanf(FILE *stream, const char *format, ...);

示例:

#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "r");

    if (file == NULL) {
        printf("Error opening file!\n");
        return 1;
    }

    int num;
    fscanf(file, "The answer is: %d", &num);  // 格式化读取文件中的数据
    printf("Read number: %d\n", num);

    fclose(file);
    return 0;
}

6. 二进制文件操作:fread()fwrite()

fread()fwrite() 用于处理二进制文件,它们可以按字节读取或写入数据。

6.1 fread()

fread() 从文件中读取数据块。

size_t fread(void *ptr, size_t size, size_t count, FILE *stream);
  • ptr:指向存储数据的内存位置。
  • size:每个数据单元的字节数。
  • count:要读取的数据单元数量。
  • stream:文件指针。

示例:

#include <stdio.h>

int main() {
    FILE *file = fopen("example.bin", "rb");
    int num;

    if (file == NULL) {
        printf("Error opening file!\n");
        return 1;
    }

    fread(&num, sizeof(int), 1, file);  // 读取一个整数

    printf("Read number: %d\n", num);

    fclose(file);
    return 0;
}

6.2 fwrite()

fwrite() 向文件中写入数据块。

size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream);

示例:

#include <stdio.h>

int main() {
    FILE *file = fopen("example.bin", "wb");
    int num = 42;

    if (file == NULL) {
        printf("Error opening file!\n");
        return 1;
    }

    fwrite(&num, sizeof(int), 1, file);  // 写入一个整数

    fclose(file);
    return 0;
}

7. 总结

  • 文件打开:使用 fopen() 打开文件,指定文件模式。
  • 文件关闭:使用 fclose() 关闭文件。
  • 字符读取:使用 fgetc() 读取字符,fgets() 读取字符串。
  • 字符写入:使用 fputc() 写入字符,fputs() 写入字符串。
  • 格式化输入输出:使用 fprintf() 写入,fscanf() 读取。
  • 二进制文件操作:使用 fread() 读取,fwrite() 写入二进制数据。

通过这些文件操作函数,C 语言提供了灵活的方式来处理文本文件和二进制文件。

发表回复 0

Your email address will not be published. Required fields are marked *