文件操作

cooolr 于 2023-05-23 发布

文件写入

#include <stdio.h>

int main() {
    FILE *fp = fopen("test.txt", "w");
    if (fp == NULL) {
        printf("Failed to open file\n");
        return 1;
    }
    char *text = "Hello World";
    fprintf(fp, "%s\n", text);
    fclose(fp);
    return 0;
}

文件读取

#include <stdio.h>

int main() {
    FILE *fp = fopen("test.txt", "r");
    char ch;
    while((ch = fgetc(fp)) != EOF) {
        printf("%c", ch);
    }
    fclose(fp);
    return 0;
}