You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.2 KiB
C++
60 lines
1.2 KiB
C++
#include "stdafx.h"
|
|
#include "FileMgr.h"
|
|
|
|
|
|
CFileMgr::CFileMgr()
|
|
{
|
|
}
|
|
|
|
|
|
CFileMgr::~CFileMgr()
|
|
{
|
|
}
|
|
|
|
void CFileMgr::ReadFileToStrVec(const std::string& filePath, std::vector<std::vector<std::string>>& data) {
|
|
std::ifstream file(filePath, std::ios::in | std::ios::binary);
|
|
if (!file.is_open()) {
|
|
return;
|
|
}
|
|
|
|
// 获取文件的大小
|
|
file.seekg(0, std::ios::end);
|
|
size_t fileSize = file.tellg();
|
|
file.seekg(0, std::ios::beg);
|
|
|
|
// 将文件内容映射到内存
|
|
char* fileBuffer = new char[fileSize];
|
|
file.read(fileBuffer, fileSize);
|
|
|
|
// 解析数据
|
|
char* lineStart = fileBuffer;
|
|
char* lineEnd = nullptr;
|
|
size_t rowIndex = 0;
|
|
while ((lineEnd = std::find(lineStart, fileBuffer + fileSize, '\n')) != fileBuffer + fileSize) {
|
|
std::vector<std::string> row;
|
|
char* valueStart = lineStart;
|
|
char* valueEnd = nullptr;
|
|
|
|
// 分割当前行
|
|
while ((valueEnd = std::find(valueStart, lineEnd, ',')) != lineEnd) {
|
|
row.push_back(std::string(valueStart, valueEnd));
|
|
valueStart = valueEnd + 1;
|
|
}
|
|
|
|
// 添加最后一个字段
|
|
row.push_back(std::string(valueStart, lineEnd));
|
|
|
|
// 将行数据添加到结果中
|
|
data.push_back(row);
|
|
|
|
// 移动到下一行
|
|
lineStart = lineEnd + 1;
|
|
++rowIndex;
|
|
}
|
|
|
|
// 清理
|
|
delete[] fileBuffer;
|
|
file.close();
|
|
}
|
|
|