0

我必须读入由“;”分隔的值 从 csv 文件...例如

2039213;Hans;Meier;12.20.1943;2.4;
4039293;Jim;Raynor;31.12.2011;3.0;

int;char[];char[];char[],float

如何使用 not string 仅 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&&'
4

1 回答 1

1

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

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

于 2013-11-01T15:43:06.433 回答