我必须读入由“;”分隔的值 从 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&&'