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

2024-07-13 1115阅读

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

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

    • 增加对文件模式的详细解释,特别是 w、a 和 wx 模式的区别。
    • 增加对 fopen() 返回值的进一步处理说明。
  2. 代码优化与注释:

    • 在代码中增加更多的注释,解释每一步的操作。
    • 提供更多示例代码,以展示 r+ 和 w+ 模式的用法。

改写与中文翻译

使用 fopen() 打开文件的示例程序

在 C 语言中,fopen() 方法用于打开指定的文件。

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

以下是使用 fopen() 打开文件的有效模式:'r'、'w'、'a'、'r+'、'w+'、'a+'。详情请参考 C 库函数 - fopen()。

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

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

示例

程序示例展示了解决方案的工作原理:

#include 
#include 
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 
    #include 
    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 
    #include 
    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 
      #include 
      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;
      }
      
    2. w+ 模式: 用于读取和写入文件,如果文件不存在则创建新文件,存在则清空内容。
      #include 
      #include 
      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;
      }
      
VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]