C++文件读写操作笔记

文件操作基础

C++通过<fstream>头文件提供文件操作支持,主要包含三个关键类:

  • ifstream:输入文件流(读取)
  • ofstream:输出文件流(写入)
  • fstream:双向文件流

文件打开模式:

|-------------|------------------|
| 模式标志 | 描述 |
| ios::in | 读取模式(默认ifstream) |
| ios::out | 写入模式(默认ofstream) |
| ios::app | 追加写入(不覆盖原有内容) |
| ios::binary | 二进制模式 |
| ios::trunc | 清空文件(默认out模式) |

文件读写

文本写入操作:

复制代码
void writeToFile(std::string fileName) {
    std::ofstream ofs(fileName, std::ios::app); // 追加写

    if (ofs) {
        for (int i = 0; i < 5; i++) {
            ofs << "content" << i << " ";
        }
    }
    else {
        std::cerr << "文件打开失败";
    }

    ofs.close();
}

文本逐行读取操作:

复制代码
void ReadLinetFile(std::string& fileName) {
	std::ifstream ifs(fileName);
	if (!ifs.is_open()) {
		std::cerr << "文件打开失败";
		return;
	}

	std::string line;
    
    // 以换行为结束标志
	while (std::getline(ifs, line)) {
		std::cout << line << std::endl;
	}
    ifs.clear();
    ifs.seekg(0, std::ios::beg);

    // 以空格或换行为结束标志
    std::string str;
    while (ifs >> str) {
        std::cout << str << std::endl;
    }

	ifs.close();
}

|-----------------------------|-----------------------------------------|
| 函数/操作 | 功能 |
| ifs.clear() | 清除failbit/eofbit 等错误标志(必须调用才能恢复正常的指针操作) |
| ifs.seekg(0, std::ios::beg) | 将输入位置指针定位到文件开头(字节偏移量为0) |
| ifs.tellg() | 获取当前读取位置(调试时可用来验证指针位置) |

二进制写操作:

复制代码
struct Record {
int id;
char name[20];
double value;
};
void writeToBinary(std::string fileName) {
    std::ofstream ofs(fileName, std::ios::binary);

    Record record = { 1, "hh", 2.32 };

    // reinterpret_cast<char*>(&record)
    if (ofs.write(reinterpret_cast<char*>(&record), sizeof(Record))) {
        std::cout << "success";
    }
    else {
        std::cerr << "error";
    }
}

reinterpret_cast<char*>(&record) 是C++中一种特定场景下的类型转换操作,用于直接将内存中的二进制数据视为字符流处理。

二进制读取操作:

复制代码
void readBinaryFile(const std::string& filename) {
    std::ifstream file(filename, std::ios::binary);

    if (!file) {
        std::cerr << "Error opening binary file" << std::endl;
        return;
    }

    Record record;
    while (file.read(reinterpret_cast<char*>(&record), sizeof(Record))) {
        std::cout << "ID: " << record.id
            << ", Name: " << record.name
            << ", Value: " << record.value << '\n';
    }
}

使用示例

从data.txt文件中读取前100个int整数:

复制代码
std::vector<int> read_first_100_text_ints(const std::string& filename) {
    std::ifstream file(filename);
    if (!file) {
        std::cerr << "Error opening file " << filename << std::endl;
        return {};
    }
    std::vector<int> result;
    result.reserve(100);
    int num;
    for (int i = 0; i < 100; ++i) {
        if (file >> num) {
            result.push_back(num);
            std::cout << num << " ";
        }
        else {
            if (file.eof()) {
                std::cout << "Reached end of file at number " << i << std::endl;
            }
            else {
                std::cerr << "Invalid data format at position " << i << std::endl;
                file.clear(); // 清除错误状态
                file.ignore(std::numeric_limits<std::streamsize>::max(), ' ');
            }
            break;
        }
    }
    return result;
}

file.ignore(std::numeric_limits<std::streamsize>::max(), ' ');

  • 定位错误 :通过i标识出错的读取操作序号(第i+1次尝试读取)
  • 流状态恢复file.clear()清除failbit等错误标志
  • 错误数据跳过ignore()跳过直到下一个空格或文件结尾

常见错误排查

|------------|-------------|-----------------|
| 错误现象 | 可能原因 | 解决方案 |
| 返回空数据 | 文件未成功打开 | 检查is_open()状态 |
| 二进制数据损坏 | 未指定binary模式 | 添加ios::binary标志 |
| 部分写入失败 | 磁盘空间不足 | 检查磁盘状态 |
| 读取和写入内容不匹配 | 平台换行符差异 | 使用'\n'代替endl |

相关推荐
kikikidult6 分钟前
(2025.07)解决——ubuntu20.04系统开机黑屏,左上角光标闪烁
笔记·ubuntu
coding随想18 分钟前
JavaScript中的BOM:Window对象全解析
开发语言·javascript·ecmascript
近津薪荼31 分钟前
初学者关于数据在内存中的储存的笔记
笔记
念九_ysl1 小时前
Java 使用 OpenHTMLToPDF + Batik 将含 SVG 遮罩的 HTML 转为 PDF 的完整实践
java·开发语言·pdf
yaoxin5211231 小时前
124. Java 泛型 - 有界类型参数
java·开发语言
liulilittle1 小时前
深度剖析:OPENPPP2 libtcpip 实现原理与架构设计
开发语言·网络·c++·tcp/ip·智能路由器·tcp·通信
88号技师1 小时前
2025年6月一区-田忌赛马优化算法Tianji’s horse racing optimization-附Matlab免费代码
开发语言·算法·matlab·优化算法
勤奋的知更鸟2 小时前
Java 编程之模板方法模式
java·开发语言·模板方法模式
十年编程老舅2 小时前
跨越十年的C++演进:C++20新特性全解析
c++·c++11·c++20·c++14·c++23·c++17·c++新特性
碎叶城李白2 小时前
若依学习笔记1-validated
java·笔记·学习·validated