我必须读取以“;”分隔的值来自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&&'
答案 0 :(得分: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