【C学习笔记】文件读写 文件IO

本文为基于“尚硅谷嵌入式技术之Linux应用层开发”的自学习笔记

一、打开文件

fopen

  • 定义:FILE *fopen(const char *filename, const char *mode);

  • 参数

    • filename:要打开的文件名(含路径)。
    • mode:文件打开模式(如"r"只读,"w"写入等)。
      • “r”: 只读模式 没有文件打开失败
      • “w”: 只写模式 存在文件写入会清空文件,不存在文件则创建新文件
      • “a”: 只追加写模式 不会覆盖原有内容 新内容写到末尾,如果文件不存在则创建
      • “r+”: 读写模式 文件必须存在 写入是从头一个一个覆盖
      • “w+”: 读写模式 可读取,写入同样会清空文件内容,不存在则创建新文件
      • “a+”: 读写追加模式 可读取,写入从文件末尾开始,如果文件不存在则创建
  • 返回值

    • 成功:指向FILE结构的指针。
    • 失败:NULL。
#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "r");
    if (file == NULL) {
        printf("无法打开文件\n");
        return 1;
    }
    fclose(file);
    return 0;
}

二、文件关闭

fclose

  • 定义:int fclose(FILE *fp);
  • 参数: fp为之前通过fopen打开的文件指针。
  • 返回值:
    • 成功:0。
    • 失败:EOF。
#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "r");
    if (file == NULL) return 1;
    // 文件操作...
    fclose(file);
    return 0;
}

三、向文件写入内容

fputc

  • 定义: int fputc(int c, FILE *stream);
  • 参数:
    • c:要写入的字符的ASCLL码值。
    • stream:文件指针。
  • 返回值:
    • 成功:写入的字符的ASCLL值。
    • 错误:EOF。
#include <stdio.h>

int main() {
    FILE *file = fopen("output.txt", "w");
    if (file == NULL) return 1;
    fputc('A', file);
    fclose(file);
    return 0;
}

fputs

  • 定义: int fputs(const char *str, FILE *stream);
  • 参数:
    • str:要写入的字符串。
    • stream:文件指针。
  • 返回值:
    • 成功:非负数。
    • 错误:EOF。
#include <stdio.h>

int main() {
    FILE *file = fopen("output.txt", "w");
    if (file == NULL) return 1;
    fputs("Hello, World!\n", file);
    fclose(file);
    return 0;
}

fprintf

  • 定义: int fprintf(FILE *stream, const char *format, ...);
  • 参数:
    • stream:文件指针。
    • format:格式化字符串。
    • …:根据格式化字符串匹配的额外参数。
  • 返回值:
    • 成功:写入字符的数量。
    • 错误:负值。
#include <stdio.h>

int main() {
    FILE *file = fopen("output.txt", "w");
    if (file == NULL) return 1;
    fprintf(file, "Number: %d\n", 42);
    fclose(file);
    return 0;
}

四、从文件中读取内容

fgetc

  • 定义: int fgetc(FILE *fp);
  • 参数: fp为文件指针。
  • 返回值:
    • 成功:从文件中读取的字符。
    • 到达文件末尾或出错:EOF。
#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "r");
    if (file == NULL) return 1;
    int c;
    while ((c = fgetc(file)) != EOF) putchar(c);
    fclose(file);
    return 0;
}

fgets

  • 定义: char *fgets(char *str, int n, FILE *stream);
  • 参数:
    • str:存储读取字符串的缓冲区。
    • n:最多读取的字符数(包含结束符\0)。
    • stream:文件指针。
  • 返回值:
    • 成功:指向str的指针。
    • 失败或到达文件末尾:NULL。
#include <stdio.h>

int main() {
    char buffer[100];
    FILE *file = fopen("example.txt", "r");
    if (file == NULL) return 1;
    while (fgets(buffer, sizeof(buffer), file)) puts(buffer);
    fclose(file);
    return 0;
}

fscanf

  • 定义: int fscanf(FILE *stream, const char *format, ...);
  • 参数:
    • stream:文件指针。
    • format:格式化字符串。
    • …:根据格式化字符串匹配的额外参数。
  • 返回值:
    • 成功:成功读取并分配的项目数量。
    • 错误或到达文件末尾:EOF。
#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "r");
    if (file == NULL) return 1;
    int number;
    while (fscanf(file, "%d", &number) == 1) {
        printf("%d\n", number);
    }
    fclose(file);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

精神小黑猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值