阅读 csv 并在分号上拆分

Read csv and split on semicolon

本文关键字:拆分 csv 阅读      更新时间:2023-10-16

我必须从 csv 文件中读取用";"分隔的值......例如

2039213;Hans;Meier;12.20.1943;2.4;
4039293;Jim;Raynor;31.12.2011;3.0;
int;char[];char[];char[],float

如何使用不是字符串,只是 char[] 将一行拆分为单词?然后我必须将这些拆分的值放入一个结构中,好吧,我认为这并不难,但是我如何拆分值?我的代码:

struct Studentendaten {
    int matrnr;
    string name;
    string vorname;
    string datum;
    float note;
};
Studentendaten stud;
array<Studentendaten,100> studArray ;   

  if (pos != -1) 
  {
      sub1 = sub.substr(0,pos);               
      sub2  = sub.substr(pos+1,pos);
      sub3  = sub.substr(pos+1,pos);
      sub4  =sub.substr(pos+1,pos);
      sub5  =sub.substr(pos+1,pos);           
      stud.matrnr = std::to_string(sub1);
      stud.name = sub2;
      stud.vorname = sub3;
      stud.datum = sub4;
      stud.note = float(sub5);
  }
  if (ch == 'n') 
  {
      stud = {matrn,name,vorname,datum,note};
      studArray[i] = stud;
      i++;
  }

我也有从字符串到 int 以及从字符串到 float 的转换不起作用的问题,无论我应用什么功能......它经常说:

dateiLesen.cc:54:19: error: 'to_string' is not a member of 'std'

 dateiLesen.cc:58:27: error: invalid cast from type 'std::string {aka std::basic_string<char>}' to type 'float'

此外,我不知道我的结构有什么问题:

 dateiLesen.cc:13:9: note: main()::Studentendaten& main()::Studentendaten::operator=(const main()::Studentendaten&)
  struct Studentendaten {
 dateiLesen.cc:13:9: note:   no known conversion for argument 1 from '<brace-enclosed initializer  list>' to 'const main()::Studentendaten&'
dateiLesen.cc:13:9: note: main()::Studentendaten& main()::Studentendaten::operator=(main()::Studentendaten&&)
dateiLesen.cc:13:9: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'main()::Studentendaten&&'

好的,让我们尝试解决这些编译错误

dateiLesen.cc:54:19: error: 'to_string' is not a member of 'std'

您没有包含定义 std::to_string 的标头,因此编译器不知道它是什么。 [ 提示:试试谷歌!

dateiLesen.cc:58:27: error: invalid cast from type 'std::string {aka std::basic_string<char>}' to type 'float'

不能直接将字符串转换为浮点数。 您需要使用类似 boost::lexical_cast