在C++开发中,将文件完整读入内存是很多场景下的基础需求,比如处理配置文件、加载二进制资源、进行文件内容分析等。不同的文件类型和性能要求对应不同的实现方案,下面详细介绍几种常用的完整读入内存方法。
文本文件完整读入内存方案
方案一:使用ifstream配合string逐行读取
这种方式适合处理文本文件,逻辑简单易懂,适合小到中等大小的文本文件。
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
// 将文本文件完整读入string
std::string readTextFileToString(const std::string& filePath) {
std::ifstream file(filePath);
if (!file.is_open()) {
throw std::runtime_error("无法打开文件: " + filePath);
}
// 将文件内容移动到stringstream,再转换为string
std::stringstream buffer;
buffer << file.rdbuf();
return buffer.str();
}
int main() {
try {
std::string content = readTextFileToString("test.txt");
std::cout << "文件内容长度: " << content.size() << std::endl;
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
return 0;
}
方案二:预分配内存提升读取效率
如果已知文件大小,提前分配string的内存可以减少内存重新分配的次数,提升读取性能。
#include <iostream>
#include <fstream>
#include <string>
std::string readTextFileWithPreAlloc(const std::string& filePath) {
std::ifstream file(filePath, std::ios::ate | std::ios::binary);
if (!file.is_open()) {
throw std::runtime_error("无法打开文件: " + filePath);
}
// 将文件指针移动到末尾,获取文件大小
std::streamsize fileSize = file.tellg();
file.seekg(0, std::ios::beg);
// 预分配string内存
std::string content(fileSize, ' ');
// 读取全部内容
if (!file.read(&content[0], fileSize)) {
throw std::runtime_error("读取文件失败: " + filePath);
}
return content;
}
二进制文件完整读入内存方案
方案一:使用vector存储二进制数据
二进制文件包含不可见字符,使用vector<char>存储是最合适的选择,避免string处理二进制数据可能出现的问题。
#include <iostream>
#include <fstream>
#include <vector>
// 将二进制文件完整读入vector<char>
std::vector<char> readBinaryFileToVector(const std::string& filePath) {
// 以二进制模式打开,ate模式直接跳到文件末尾
std::ifstream file(filePath, std::ios::binary | std::ios::ate);
if (!file.is_open()) {
throw std::runtime_error("无法打开二进制文件: " + filePath);
}
// 获取文件大小
std::streamsize fileSize = file.tellg();
file.seekg(0, std::ios::beg);
// 创建对应大小的vector
std::vector<char> buffer(fileSize);
// 读取全部内容到vector
if (!file.read(buffer.data(), fileSize)) {
throw std::runtime_error("读取二进制文件失败: " + filePath);
}
return buffer;
}
int main() {
try {
std::vector<char> binaryData = readBinaryFileToVector("test.bin");
std::cout << "二进制文件大小: " << binaryData.size() << " 字节" << std::endl;
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
return 0;
}
方案二:使用自定义缓冲区读取
如果需要在读取过程中做额外处理,可以使用自定义缓冲区循环读取,适合超大文件分块处理的场景,这里演示完整读入的写法。
#include <iostream>
#include <fstream>
#include <vector>
std::vector<char> readBinaryFileWithBuffer(const std::string& filePath) {
std::ifstream file(filePath, std::ios::binary);
if (!file.is_open()) {
throw std::runtime_error("无法打开文件: " + filePath);
}
std::vector<char> buffer;
// 每次读取1KB数据
const int BUFFER_SIZE = 1024;
char tempBuffer[BUFFER_SIZE];
while (file.read(tempBuffer, BUFFER_SIZE)) {
buffer.insert(buffer.end(), tempBuffer, tempBuffer + file.gcount());
}
// 读取剩余不足1KB的数据
if (file.gcount() > 0) {
buffer.insert(buffer.end(), tempBuffer, tempBuffer + file.gcount());
}
return buffer;
}
不同方案对比
下面是几种方案的适用场景和优缺点对比:
| 方案 | 适用场景 | 优点 | 缺点 |
|---|---|---|---|
| ifstream配合stringstream | 小到中等文本文件 | 代码简洁,逻辑清晰 | 性能一般,不适合超大文件 |
| 预分配内存读取文本 | 已知大小的文本文件 | 性能较好,减少内存分配 | 需要提前获取文件大小 |
| vector读取二进制 | 所有二进制文件 | 安全稳定,适合存储二进制数据 | 无明显缺点 |
| 自定义缓冲区读取 | 超大文件,需要分块处理 | 灵活,可中途处理数据 | 代码稍复杂 |
注意事项
- 读取二进制文件时必须指定
std::ios::binary模式,否则可能出现换行符转换导致内容错误 - 处理大文件时要注意内存占用,避免一次性读入超过可用内存的文件
- 打开文件后要检查是否打开成功,读取后要检查是否读取完整
- 文本文件读取如果使用string,注意string不会存储末尾的空字符,二进制数据如果需要保留空字符,不要使用string存储
文件读入内存后,可以根据具体需求进行解析、处理或者转换,上述方案都是经过验证的可靠实现,开发者可以根据实际场景选择使用。