首页 > 后端开发 > C++ > 正文

C 文件处理基础知识

PHPz
发布: 2023-09-16 08:29:02
转载
1550人浏览过

c 文件处理基础知识

在这里我们将看到一些在C语言中的基本文件处理操作。以下是这些操作的列表:

  • 向文件中写入内容
  • 从文件中读取内容
  • 在文件中追加内容

向文件中写入内容

请参考以下代码以了解如何向文件中写入内容

示例代码

#include <stdio.h>
int main() {
   FILE *fp;
   char *filename = "sample.txt";
   char *content = "Hey there! You've successfully created a file with content in c programming language.";
   /* open for writing */
   fp = fopen(filename, "w");
   if( fp == NULL ) {
      printf("%s: failed to open. </p><p>", filename);
      return -1;
   } else {
      printf("%s: opened in write mode.</p><p>", filename);
   }
   /* Write content to file */
   fprintf(fp, "%s</p><p>", content);
   if( !fclose(fp) )
      printf("%s: closed successfully.</p><p>", filename);
   return 0;
}
登录后复制

输出

sample.txt: opened in write mode.
sample.txt: closed successfully.
登录后复制

2.从文件中读取

查看代码以了解我们如何从文件中读取 创建一个文件(file_read.txt):

您使用C编程语言打开了一个只读模式的文件。

示例代码

#include <stdio.h>
int main() {
   FILE *fp;
   char *filename = "file_read.txt";
   char ch;
   /* open for writing */
   fp = fopen(filename, "r");
   if (fp == NULL) {
      printf("%s does not exists </p><p>", filename);
      return;
   } else {
      printf("%s: opened in read mode.</p><p></p><p>", filename);
   }
   while ((ch = fgetc(fp) )!= EOF) {
      printf ("%c", ch);
   }
   if (!fclose(fp))
      printf("</p><p>%s: closed.</p>
                    <div class="aritcle_card">
                        <a class="aritcle_card_img" href="/ai/923">
                            <img src="https://img.php.cn/upload/ai_manual/000/000/000/175679997247874.jpg" alt="知我AI">
                        </a>
                        <div class="aritcle_card_info">
                            <a href="/ai/923">知我AI</a>
                            <p>一款多端AI知识助理,通过一键生成播客/视频/文档/网页文章摘要、思维导图,提高个人知识获取效率;自动存储知识,通过与知识库聊天,提高知识利用效率。</p>
                            <div class="">
                                <img src="/static/images/card_xiazai.png" alt="知我AI">
                                <span>26</span>
                            </div>
                        </div>
                        <a href="/ai/923" class="aritcle_card_btn">
                            <span>查看详情</span>
                            <img src="/static/images/cardxiayige-3.png" alt="知我AI">
                        </a>
                    </div>
                <p>", filename);
   return 0;
}
登录后复制

输出

file_read.txt: opened in read mode.
You have opened a file using C programming language, in read-only mode.
file_read.txt: closed.
登录后复制

3.将内容追加到文件中

查看代码以了解如何将行追加到文件中的方法。

创建文件(file_append.txt)

This text was already there in the file.
登录后复制

示例代码

#include <stdio.h>
int main() {
   FILE *fp;
   char ch;
   char *filename = "file_append.txt";
   char *content = "This text is appeneded later to the file, using C programming.";
   /* open for writing */
   fp = fopen(filename, "r");
   printf("</p><p>Contents of %s -</p><p></p><p>", filename);
   while ((ch = fgetc(fp) )!= EOF) {
      printf ("%c", ch);
   }
   fclose(fp);
   fp = fopen(filename, "a");
   /* Write content to file */
   fprintf(fp, "%s</p><p>", content);
   fclose(fp);
   fp = fopen(filename, "r");
   printf("</p><p>Contents of %s -</p><p>", filename);
   while ((ch = fgetc(fp) )!= EOF) {
      printf ("%c", ch);
   }
   fclose(fp);
   return 0;
}
登录后复制

输出

Contents of file_append.txt -
This text was already there in the file.
Appending content to file_append.txt...
Content of file_append.txt after 'append' operation is -
This text was already there in the file.
This text is appeneded later to the file, using C programming.
登录后复制

以上就是C 文件处理基础知识的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号