c++ - c++ 找到空格时将文件拆分为记录?
问题描述
我有这样的文件
2.7388 0 2.4670
3.6384 1 24.5927
8.7844 0 10.6871
11.2892 0 2.6507
19.6054 1 16.8974
25.8811 0 8.0084
例如,我将占据第一行2.7388 0 2.4670
。我试图完成类似以下的事情
2.7388 = double x
0 = int y
2.4670 = double z
我的尝试
struct fileReader{
double x;
int y;
double z;
};
std::vector<fileReader> afileReader(std::string fileName) {
std::vector<fileReader> readtheFile;
std::ifstream List;
std::string line;
List.open(fileName);
char delimiter = ' '; //delimiter added
while (std::getline(List, line))
{
line.pop_back();
std::string token;
std::istringstream ss(line);
fileReader afileReader;
std::getline(ss, token,delimiter);
afileReader.x = std::stod(token); //change from stoi to stod
std::getline(ss, token,delimiter);
afileReader.y = std::stoi(token);
std::getline(ss, token,delimiter);
afileReader.z = std::stod(token); //change from stoi to stod
readtheFile.push_back(std::move(afileReader));
}
for(fileReader x: readtheFile) {
std::cout << x.x;
std::cout << x.y;
std::cout << x.z;
}
return readtheFile;
passengerList.close();
}
当我从 main 运行这条线时afileReader("file.txt")
。我收到以下错误
terminate called after throwing an instance of 'std::invalid_argument'
what(): stod //error changed
Aborted (core dumped)
我不知道为什么我得到一个错误 stoi 作为将 int 值转换为字符串的正确转换方法(因为令牌是字符串)
回到这个问题,虽然我希望在空格被击中时拆分记录。所以回到前面的例子2.7388 0 2.4670
,什么时候2.7388
被读取它分配给变量x
,什么时候0
被读取它分配给变量y
并被2.4670
分配给变量z
解决方案
(推荐)更直接的方法:
#include <fstream>
#include <iostream>
#include <vector>
struct Data {
double x;
int y;
double z;
};
std::vector<Data> fileReader(std::string fileName) {
std::vector<Data> result;
std::ifstream file(fileName);
Data data;
while (file >> data.x >> data.y >> data.z)
result.push_back(data);
for (auto &&i : result)
std::cout << i.x << ' ' << i.y << ' ' << i.z << '\n';
return result;
}
int main() { fileReader("test.txt"); }
您的原始代码存在问题:
fileReader::x
并且fileReader::z
不是浮点型数字。' '
没有作为分隔符传递给std::getline
.- 您正在阅读的文件中的间距不一致。(修复)
- 出于某种奇怪的原因,您正在这样做
line.pop_back()
; - [某些警告的原因] 您没有从
afileReader()
(非无效函数)返回任何内容。
固定代码:
#include <fstream>
#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
struct fileReader {
double x;
int y;
double z;
};
std::vector<fileReader> afileReader(std::string fileName) {
std::vector<fileReader> readtheFile;
std::ifstream List(fileName);
std::string line;
while (std::getline(List, line)) {
std::string token;
std::istringstream ss(line);
fileReader afileReader;
ss >> std::ws;
std::getline(ss, token, ' ');
afileReader.x = std::stod(token);
ss >> std::ws;
std::getline(ss, token, ' ');
afileReader.y = std::stoi(token);
ss >> std::ws;
std::getline(ss, token);
afileReader.z = std::stod(token);
readtheFile.push_back(std::move(afileReader));
}
for (auto &&i : readtheFile)
std::cout << i.x << ' ' << i.y << ' ' << i.z << '\n';
return readtheFile;
}
int main() { afileReader("test.txt"); }
推荐阅读
- excel - 需要 Excel、VBA 和用户表单方面的帮助,特别是在写入现有数据时
- java - 如何将字符串列表转换为 LinkedHashMap?
- html - 检查单选时检查文本颜色
- github - 我搞砸了主分支并且项目无法加载,我如何创建一个新的主分支?
- node.js - 使用 promise 将一个 NodeJS module.export 函数的结果传递给另一个 NodeJS module.export 函数
- powershell - 如何在 Windows 批处理文件中传递字符串数组
- mongodb - 如何在 MongoDB 聚合管道中查找并返回结果所在的页面?
- javascript - 在 React 中显示/隐藏组件
- excel - VBA - 隐藏/取消隐藏行作为工作表事件的速度
- python - 将元组列表(int,float)写入流而不转换为字符串