当前位置: 首页 > 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.lryc.cn/news/596609.html

相关文章:

  • wangEditor5添加键盘事件/实现定时保存功能
  • 单张显卡运行多个vllm模型
  • 进程优先级切换调度-进程概念(6)
  • 【C++】继承和多态扩展学习
  • PyQt5在Pycharm上的环境搭建 -- Qt Designer + Pyuic + Pyrcc组合,大幅提升GUI开发效率
  • Qt多语言支持初步探索
  • 按键精灵脚本:自动化利刃的双面性 - 从技术原理到深度实践与反思
  • Web3面试题
  • 拥抱区块链红利:机遇无限,风险暗涌
  • 期权分红怎么分的?
  • UNet改进(24):注意力机制-从基础原理到高级融合策略
  • Atcoder Beginner Contest 415 D题
  • 算法笔记之堆排序
  • 2023CCPC秦皇岛 F. Mystery of Prime(线性DP)
  • Python通关秘籍(四)数据结构——列表
  • iView Table组件二次封装
  • Elasticsearch服务器开发(第2版) - 读书笔记 第一章 Elasticsearch集群入门
  • 【uboot/kernel1】启动流程,环境变量,内存,initramfs
  • 【数学建模】基础知识
  • 【Verilog】竞争、冒险
  • 本地大模型VRAM需求计算器:原理与实现详解
  • Web3介绍(Web 3.0)(一种基于区块链技术的去中心化互联网范式,旨在通过技术手段实现用户对数据的自主权、隐私保护和价值共享)
  • 浙江大学PTA程序设计C语言基础编程练习题1-5
  • 高并发场景下的缓存问题与一致性解决方案(技术方案总结)
  • Redis 初识
  • Vue项目中的AJAX请求与跨域问题解析
  • Trae安装指定版本的插件
  • 网络编程---TCP协议
  • 浏览器解码顺序xss
  • Matlab学习笔记:界面使用