问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

C语言中使用fopen()打开和操作文件的详细指南

创作时间:
作者:
@小白创作中心

C语言中使用fopen()打开和操作文件的详细指南

引用
CSDN
1.
https://m.blog.csdn.net/mzgxinhua/article/details/139688304

文件模式的解释与详细说明

在C语言中,fopen()函数用于打开指定的文件。其基本语法如下:

FILE *fopen(const char *filename, const char *mode);

其中,filename是要打开的文件名,mode是打开文件的方式。以下是fopen()支持的有效模式:

  • 'r':以只读方式打开文件,文件必须存在。
  • 'w':以写方式打开文件,若文件存在则清空文件内容,若文件不存在则创建新文件。
  • 'a':以追加方式打开文件,写入的数据会添加到文件末尾,若文件不存在则创建新文件。
  • 'r+':以读写方式打开文件,文件必须存在。
  • 'w+':以读写方式打开文件,若文件存在则清空文件内容,若文件不存在则创建新文件。
  • 'a+':以读写方式打开文件,写入的数据会添加到文件末尾,若文件不存在则创建新文件。

fopen()返回值的处理

fopen()函数返回一个指向FILE类型的指针,如果打开文件失败,则返回NULL。因此,在使用fopen()打开文件后,应该检查返回值是否为NULL,以确保文件成功打开。

代码示例

使用fopen()以写模式打开现有文件

如果要打开的文件不存在于当前目录中,则会创建一个新的空文件,并以写模式打开。如果文件存在且以'w''w+'模式打开,则在写操作之前文件内容会被删除。

示例代码:

#include <stdio.h>
#include <stdlib.h>

int main() {
    // 以写模式打开文件
    FILE *opFile = fopen("test.txt", "w");
    if (opFile == NULL) {
        puts("Couldn't open file");
        exit(0);
    } else {
        fputs("includehelp", opFile);
        puts("Write operation successful");
        fclose(opFile);
    }
    return 0;
}

输出:

Write operation successful

说明:初始文件内容 - C programming language
写操作后的内容 - includehelp
写操作会删除文件中已有的所有内容,然后写入新内容。为了解决这个问题,C语言提供了两种不同的方法,程序员可以根据需要选择:

  • 'a'(追加模式)- 这种模式会将新内容追加到文件内容的末尾。
  • 'wx'模式 - 如果文件已存在于目录中,则返回NULL

使用'a'模式对现有文件进行写操作

示例代码:

#include <stdio.h>
#include <stdlib.h>

int main() {
    // 以追加模式打开文件
    FILE *opFile = fopen("test.txt", "a");
    if (opFile == NULL) {
        puts("Couldn't open file");
        exit(0);
    } else {
        fputs("includehelp", opFile);
        puts("Write operation successful");
        fclose(opFile);
    }
    return 0;
}

输出:

Write operation successful

说明:初始文件内容 - C programming language
追加操作后的内容 - C programming language includehelp

使用'wx'模式对现有文件进行写操作

示例代码:

#include <stdio.h>
#include <stdlib.h>

int main() {
    // 以 'wx' 模式打开文件
    FILE *opFile = fopen("test.txt", "wx");
    if (opFile == NULL) {
        puts("Couldn't open file");
        exit(0);
    } else {
        fputs("includehelp", opFile);
        puts("Write operation successful");
        fclose(opFile);
    }
    return 0;
}

输出:

Write operation successful

说明:使用'wx'模式,如果文件已存在,则会返回NULL并退出,不会覆盖文件内容。

补充示例:r+w+模式的使用

  1. **r+模式:**用于读取和写入文件,但文件必须存在。

示例代码:

#include <stdio.h>
#include <stdlib.h>

int main() {
    // 以 'r+' 模式打开文件
    FILE *opFile = fopen("test.txt", "r+");
    if (opFile == NULL) {
        puts("Couldn't open file");
        exit(0);
    } else {
        fputs(" new text", opFile);
        puts("Write operation successful");
        fclose(opFile);
    }
    return 0;
}
  1. **w+模式:**用于读取和写入文件,如果文件不存在则创建新文件,存在则清空内容。

示例代码:

#include <stdio.h>
#include <stdlib.h>

int main() {
    // 以 'w+' 模式打开文件
    FILE *opFile = fopen("test.txt", "w+");
    if (opFile == NULL) {
        puts("Couldn't open file");
        exit(0);
    } else {
        fputs("includehelp", opFile);
        puts("Write operation successful");
        fclose(opFile);
    }
    return 0;
}

本文原文来自CSDN

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号