阅读 csv 并在分号上拆分
Read csv and split on semicolon
我必须从 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
相关文章:
- 正在将csv文件读取为双精度矢量
- C++将向量的向量拆分为向量的N个子向量
- 如何拆分文件中.txt字母并使用c ++使用数组进行扑克?
- 如何选择在 csv 文件中输出的位置
- OpenCV 4.1.2 - 从网络摄像头获取帧并将其拆分
- 使用 C++ 将命令行参数拆分为参数/向量
- C++(.cpp文件和.h文件)拆分代码并添加一个函数,提取 - 这很容易吗?
- 如何在C++中将数字拆分为数字
- 在线程之间拆分任务总是值得的吗?
- 将字符串拆分为标记,并将标记拆分为两个单独的数组
- 在 c++ 中拆分类和标头中的继承,错误
- 在矢量内拆分 deques
- 拆分重叠段
- 有没有办法在不使用 getline() 的情况下从.csv文件中读取?
- 我还可以添加什么来按空格或空格正确拆分C++字符串?
- 通过空行提升拆分字符串
- 如何从 CSV 获取数据并将其存储在 C++ 中的表对象中
- 无法读取C++中的 CSV 文件
- 阅读 csv 并在分号上拆分
- C++--文件名中的前导零(CSV文件)--存储/拆分std::矢量的值