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

C++基础学习——文件操作详解

一、文件流类概述

C++ 标准库提供了三个主要的文件流类:

  1. ifstream (输入文件流):用于从文件读取数据
  2. ofstream (输出文件流):用于向文件写入数据
  3. fstream (文件流):既可读又可写

这些类都继承自 iostream 类,因此可以使用 <<>> 操作符进行格式化I/O操作。

二、文件打开与关闭

1. 打开文件

#include <fstream>
using namespace std;// 方法1:先声明再打开
ofstream outFile;
outFile.open("output.txt");// 方法2:声明时直接打开
ifstream inFile("input.txt");// 方法3:使用fstream
fstream ioFile;
ioFile.open("data.txt", ios::in | ios::out);

2. 文件打开模式

模式标志描述
ios::in以读取方式打开
ios::out以写入方式打开
ios::app追加模式,所有写入都追加到文件末尾
ios::ate打开文件后定位到文件末尾
ios::trunc如果文件存在则清空内容
ios::binary二进制模式

组合示例:

// 以写入和追加模式打开
ofstream logFile("log.txt", ios::out | ios::app);// 以读写和二进制模式打开
fstream dataFile("data.dat", ios::in | ios::out | ios::binary);

3. 关闭文件

outFile.close();
inFile.close();
ioFile.close();

注意:虽然流对象析构时会自动关闭文件,但显式关闭是个好习惯。

三、文件状态检查

ifstream file("data.txt");if (file.is_open()) {// 文件成功打开
} else {// 文件打开失败
}// 检查流状态
if (file.good()) {// 流状态良好
}if (file.fail()) {// 操作失败(非致命错误)
}if (file.bad()) {// 严重错误
}if (file.eof()) {// 到达文件末尾
}

四、文本文件操作

1. 写入文本文件

ofstream out("output.txt");
if (out.is_open()) {out << "第一行文本" << endl;out << 123 << " " << 3.14 << endl;out << "这是另一行文本" << endl;out.close();
} else {cerr << "无法打开文件!" << endl;
}

2. 读取文本文件

逐行读取:
ifstream in("input.txt");
string line;
if (in.is_open()) {while (getline(in, line)) {cout << line << endl;}in.close();
}
逐词读取:
ifstream in("input.txt");
string word;
if (in.is_open()) {while (in >> word) {cout << word << endl;}in.close();
}
格式化读取:
ifstream in("data.txt");
int id;
double value;
string name;if (in.is_open()) {while (in >> id >> value >> name) {cout << "ID: " << id << ", Value: " << value << ", Name: " << name << endl;}in.close();
}

五、二进制文件操作

1. 写入二进制数据

struct Record {int id;char name[50];double salary;
};Record emp = {101, "John Doe", 4500.50};ofstream out("employees.dat", ios::binary);
if (out.is_open()) {out.write(reinterpret_cast<char*>(&emp), sizeof(Record));out.close();
}

2. 读取二进制数据

Record emp;
ifstream in("employees.dat", ios::binary);
if (in.is_open()) {in.read(reinterpret_cast<char*>(&emp), sizeof(Record));cout << "ID: " << emp.id << endl;cout << "Name: " << emp.name << endl;cout << "Salary: " << emp.salary << endl;in.close();
}

3. 二进制文件随机访问

fstream file("data.dat", ios::in | ios::out | ios::binary);// 写入多个记录
Record records[3] = {{101, "Alice", 5000},{102, "Bob", 6000},{103, "Charlie", 7000}
};file.write(reinterpret_cast<char*>(records), 3 * sizeof(Record));// 直接读取第二个记录
Record emp;
file.seekg(1 * sizeof(Record), ios::beg);
file.read(reinterpret_cast<char*>(&emp), sizeof(Record));// 修改第三个记录
emp = {103, "David", 7500};
file.seekp(2 * sizeof(Record), ios::beg);
file.write(reinterpret_cast<char*>(&emp), sizeof(Record));file.close();

六、文件定位

1. 获取当前位置

streampos pos = file.tellg();  // 获取读取位置
streampos pos = file.tellp();  // 获取写入位置

2. 设置位置

// 绝对定位
file.seekg(0, ios::beg);  // 移动到文件开头
file.seekg(0, ios::end);  // 移动到文件末尾// 相对定位
file.seekg(10, ios::cur);  // 从当前位置向前移动10字节
file.seekg(-5, ios::cur);  // 从当前位置向后移动5字节

七、实用文件操作函数

1. 检查文件是否存在

#include <sys/stat.h>bool fileExists(const string& filename) {struct stat buffer;return (stat(filename.c_str(), &buffer) == 0);
}

2. 获取文件大小

streampos getFileSize(const string& filename) {ifstream file(filename, ios::ate | ios::binary);return file.tellg();
}

3. 复制文件

bool copyFile(const string& source, const string& dest) {ifstream src(source, ios::binary);ofstream dst(dest, ios::binary);if (!src || !dst) {return false;}dst << src.rdbuf();return true;
}

4. 读取CSV文件

void readCSV(const string& filename) {ifstream file(filename);string line;while (getline(file, line)) {stringstream ss(line);string cell;vector<string> row;while (getline(ss, cell, ',')) {row.push_back(cell);}// 处理行数据for (const auto& val : row) {cout << val << "\t";}cout << endl;}
}

八、错误处理最佳实践

void processFile(const string& filename) {ifstream file(filename);if (!file) {cerr << "错误:无法打开文件 " << filename << endl;perror("原因");  // 打印系统错误信息return;}try {// 文件处理代码string line;while (getline(file, line)) {// 处理每一行}if (file.bad()) {throw runtime_error("读取文件时发生严重错误");}if (file.fail() && !file.eof()) {throw runtime_error("文件格式错误");}} catch (const exception& e) {cerr << "错误: " << e.what() << endl;file.close();return;}file.close();
}

九、性能考虑

  1. 缓冲区大小:默认缓冲区大小可能不适合大文件操作,可以自定义:

    char buffer[8192];  // 8KB缓冲区
    ifstream file;
    file.rdbuf()->pubsetbuf(buffer, sizeof(buffer));
    file.open("largefile.dat");
    
  2. 二进制 vs 文本:二进制操作通常比文本操作更快,特别是对于大量数据。

  3. 内存映射文件:对于极大文件,考虑使用操作系统特定的内存映射文件API。

十、C++17 文件系统库

C++17 引入了 <filesystem> 库,提供了更高级的文件操作:

#include <filesystem>
namespace fs = std::filesystem;// 检查文件是否存在
if (fs::exists("test.txt")) {// 获取文件大小auto size = fs::file_size("test.txt");// 复制文件fs::copy("source.txt", "destination.txt");// 删除文件fs::remove("old_file.txt");// 遍历目录for (auto& entry : fs::directory_iterator(".")) {cout << entry.path() << endl;}
}

通过掌握这些文件操作技术,您可以在C++程序中高效地处理各种文件I/O任务。

http://www.dtcms.com/a/292851.html

相关文章:

  • netframe4.5 的mvc 框架 layui 组件的引用
  • 模运算常见定律
  • .net 警告【代码 CS1998】此异步方法缺少 “await“ 运算符,将以同步方式运行。
  • Linux命令集锦-个人整理(偏向进程和端口的查询)
  • CS231n-2017 Lecture5卷积神经网络笔记
  • 如何把jar包打成docker镜像(SpringBoot项目打包成Docker )部署到Linux
  • CMOS知识点 离子注入工艺
  • OpenCV Mat UMat GpuMat Matx HostMem InputArray等设计哲学
  • Arduino学习笔记【快速入门】
  • 蓝牙通信架构(Bluetooth/BLE)
  • Windows系统暂停更新工具
  • 每日面试题12:JVM垃圾回收机制
  • 分布式数据库中间件ShardingSphere
  • Unity UI的未来之路:从UGUI到UI Toolkit的架构演进与特性剖析(1)
  • Java学习-----Bean
  • Datawhale AI 夏令营-心理健康Agent开发学习-Task1
  • 猎板 PCB:多场景适配下印制线路板的材料选择优化策略
  • 朴素贝叶斯算法原理与案例解析
  • linux: tar解压之后属主和属组不是当前用户问题
  • 2025人形机器人动捕技术研讨会即将于7月31日盛大开启
  • 阿里巴巴视觉算法面试30问全景精解
  • 知识库搭建之Meilisearch‘s 搜索引擎-创建搜索引擎项目 测评-东方仙盟测评师
  • 数据降噪/生物信号强化/缓解 dropout,深度学习模型 SUICA 实现空间转录组切片中任一位置基因表达的预测
  • [LLM]Synthetic Visual Genome
  • GNU到底是什么,与Unix和Linux是什么关系
  • 链表经典算法题
  • web复习
  • 网络原理 HTTP 和 HTTPS
  • kafka查看消息的具体内容 kafka-dump-log.sh
  • Python笔记完整版