首页 > 解决方案 > 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

标签: c++readfilecoredump

解决方案


(推荐)更直接的方法:

#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"); }

样品运行


您的原始代码存在问题:

  1. fileReader::x并且fileReader::z不是浮点型数字。
  2. ' '没有作为分隔符传递给std::getline.
  3. 您正在阅读的文件中的间距不一致。(修复
  4. 出于某种奇怪的原因,您正在这样做line.pop_back()
  5. [某些警告的原因] 您没有从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"); }

推荐阅读