当前位置: 首页 > news >正文

C++ 入门第26天:文件与流操作基础

往期回顾:

C++ 入门第23天:Lambda 表达式与标准库算法入门-CSDN博客

C++ 入门第24天:C++11 多线程基础-CSDN博客

C++ 入门第25天:线程池(Thread Pool)基础-CSDN博客


 C++ 入门第26天:文件与流操作基础

前言

文件是程序中用来存储数据的常用工具。在 C++ 中,文件操作是通过流(Stream)来实现的。C++ 提供了一组标准库类(如 ifstreamofstreamfstream)用于文件读写操作。

今天,我们将学习如何使用这些工具进行文件的读写,以及处理文件操作中的一些常见问题。

1. 文件流的基本概念

在 C++ 中,文件操作是通过以下三种流来实现的:

  1. ifstream:输入文件流,用于读取文件。
  2. ofstream:输出文件流,用于写入文件。
  3. fstream:文件流,可同时用于读写文件。

文件流需要头文件 <fstream>

2. 写入文件

示例代码

#include <iostream>
#include <fstream>
using namespace std;int main() {ofstream outfile("example.txt"); // 打开文件以写入if (!outfile) {cerr << "Error: Unable to open file for writing!" << endl;return 1;}// 写入内容outfile << "Hello, C++ File Operations!" << endl;outfile << "This is a second line." << endl;// 关闭文件outfile.close();cout << "File written successfully!" << endl;return 0;
}

注:

  • ofstream outfile("example.txt");:以写模式打开 example.txt 文件。如果文件不存在,将自动创建。
  • outfile << "内容";:将数据写入文件。
  • outfile.close();:关闭文件,释放资源。

运行结果: 程序运行后,将在当前目录下生成一个名为 example.txt 的文件,文件内容为:

Hello, C++ File Operations!
This is a second line.

3. 读取文件

示例代码

#include <iostream>
#include <fstream>
using namespace std;int main() {ifstream infile("example.txt"); // 打开文件以读取if (!infile) {cerr << "Error: Unable to open file for reading!" << endl;return 1;}string line;// 按行读取文件while (getline(infile, line)) {cout << line << endl;}// 关闭文件infile.close();return 0;
}

注:

  • ifstream infile("example.txt");:以读模式打开 example.txt 文件。
  • getline(infile, line);:按行读取文件内容。
  • infile.close();:关闭文件。

运行结果: 程序将输出文件 example.txt 的内容:

Hello, C++ File Operations!
This is a second line.

4. 同时读写文件

使用 fstream 类可以同时对文件进行读写操作。

示例代码

#include <iostream>
#include <fstream>
using namespace std;int main() {fstream file("example.txt", ios::in | ios::out | ios::app); // 以读写追加模式打开文件if (!file) {cerr << "Error: Unable to open file!" << endl;return 1;}// 写入新内容file << "Appending a new line to the file." << endl;// 将文件指针移到文件开始位置file.seekg(0, ios::beg);// 读取文件内容string line;while (getline(file, line)) {cout << line << endl;}// 关闭文件file.close();return 0;
}

 

  • ios::in:读模式。
  • ios::out:写模式。
  • ios::app:追加模式,将写入内容添加到文件末尾。
  • file.seekg(0, ios::beg);:将文件指针移动到文件开头,以便读取文件内容。

5. 文件操作常见问题

5.1 检查文件是否存在

#include <fstream>
#include <iostream>
using namespace std;int main() {ifstream infile("example.txt");if (infile) {cout << "File exists!" << endl;} else {cout << "File does not exist!" << endl;}infile.close();return 0;
}

5.2 删除文件

C++ 提供了 remove 函数用于删除文件。

#include <cstdio> // 包含 remove 函数
#include <iostream>
using namespace std;int main() {if (remove("example.txt") == 0) {cout << "File deleted successfully!" << endl;} else {perror("Error deleting file");}return 0;
}

6. 总结

以上就是 C++ 11 中文件与流操作的基础知识点了。文件流的类型ifstreamofstreamfstream文件读写操作:如何打开文件、写入内容、读取内容。文件操作技巧:检查文件是否存在和删除文件。文件操作是开发中必不可少的技能,可以用于日志记录、配置文件处理等多种场景。在实际应用中,还需要注意文件路径、权限和异常处理等问题。

都看到这里了,点个赞再走呗朋友~

加油吧,预祝大家变得更强!

http://www.lryc.cn/news/518067.html

相关文章:

  • 使用python将多个Excel表合并成一个表
  • halcon三维点云数据处理(七)find_shape_model_3d_recompute_score
  • vue js实现时钟以及刻度效果
  • unity学习15:预制体prefab
  • 基于Thinkphp6+uniapp的陪玩陪聊软件开发方案分析
  • MySQL - 子查询和相关子查询详解
  • Android 系统签名 keytool-importkeypair
  • 安卓漏洞学习(十八):Android加固基本原理
  • Docker 使用Dockerfile创建镜像
  • 【Python运维】利用Python实现高效的持续集成与部署(CI/CD)流程
  • 成功!QT 5.15.2编译mysql驱动
  • 安卓NDK视觉开发——手机拍照文档边缘检测实现方法与库封装
  • 第二届 Sui 游戏峰会将于 3 月 18 日在旧金山举行
  • 自动驾驶相关知识学习笔记
  • uniapp - 基于uniapp+vue3实现自定义增强版table表格组件体验「兼容H5+小程序+App端」
  • 新时期下k8s 网络插件calico 安装
  • 【SQL】COUNT()函数 用法详解
  • 【HTML+CSS+JS+VUE】web前端教程-6-图片路径详解
  • C++中面向对象的三大特性是什么?
  • Centos 修改 yum 源为阿里云
  • Qt之Cannot create children for a parent that is in a different thread问题分析
  • 均值滤波从图像复原角度的解释
  • Tableau数据可视化与仪表盘搭建-数据连接
  • VsCode对Arduino的开发配置
  • 2024版idea 插件无法加载
  • VLMs之Agent之CogAgent:CogAgent的简介、安装和使用方法、案例应用之详细攻略
  • Unity3D仿星露谷物语开发19之库存栏丢弃及交互道具
  • Kafka优势剖析-消费者组、并行消费
  • Docker+Jmeter+InfluxDB+Grafana 搭建性能监控平台
  • Maven 详细配置:Maven settings 配置文件的详细说明