c++ 从.csv文件中读取
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16446665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c++ Read from .csv file
提问by Mr. Phil
I have this code which is supposed to cout in console the information from the .csv file;
我有这个代码,它应该在控制台中输出 .csv 文件中的信息;
while(file.good())
{
getline(file, ID, ',');
cout << "ID: " << ID << " " ;
getline(file, nome, ',') ;
cout << "User: " << nome << " " ;
getline(file, idade, ',') ;
cout << "Idade: " << idade << " " ;
getline(file, genero, ' ') ;
cout << "Sexo: " << genero<< " " ;
}
And a csv file that has this (when I open with notepad):
和一个有这个的 csv 文件(当我用记事本打开时):
0,Filipe,19,M
1,Maria,20,F
2,Walter,60,M
Whenever I run the program the console will display this:
每当我运行程序时,控制台都会显示:
My question is why isn't the program repeating those cout messages in every line instead of only in the first one
我的问题是为什么程序不在每一行而不是只在第一行重复这些 cout 消息
Btw , nome is name, idade is age, and genero/sexo is gender, forgot to translate before creating this post
顺便说一句,nome 是名字,idade 是年龄,genero/sexo 是性别,在创建这篇文章之前忘记翻译了
回答by jxh
You can follow this answerto see many different ways to process CSV in C++.
您可以按照此答案查看在 C++ 中处理 CSV 的许多不同方法。
In your case, the last call to getline
is actually putting the last field of the first line and then all of the remaining lines into the variable genero
. This is because there is no space delimiter found up until the end of file. Try changing the space character into a newline instead:
在您的情况下,最后一次调用getline
实际上是将第一行的最后一个字段和所有剩余的行放入变量中genero
。这是因为在文件末尾之前没有找到空格分隔符。尝试将空格字符改为换行符:
? ? getline(file, genero, file.widen('\n'));
or more succinctly:
或更简洁地说:
getline(file, genero);
In addition, your check for file.good()
is premature. The last newline in the file is still in the input stream until it gets discarded by the next getline()
call for ID
. It is at this point that the end of file is detected, so the check should be based on that. You can fix this by changing your while
test to be based on the getline()
call for ID
itself (assuming each line is well formed).
此外,您的检查file.good()
还为时过早。文件中的最后一个换行符仍在输入流中,直到下一次getline()
调用ID
. 正是在这一点上检测到文件末尾,因此检查应基于此。您可以通过将while
测试更改为基于对自身的getline()
调用来解决此问题ID
(假设每行都格式正确)。
while (getline(file, ID, ',')) {
cout << "ID: " << ID << " " ;
getline(file, nome, ',') ;
cout << "User: " << nome << " " ;
getline(file, idade, ',') ;
cout << "Idade: " << idade << " " ;
getline(file, genero);
cout << "Sexo: " << genero<< " " ;
}
For better error checking, you should check the result of each call to getline()
.
为了更好地进行错误检查,您应该检查每次调用getline()
.
回答by AndersK
a csv-file is just like any other file a stream of characters. the getline reads from the file up to a delimiter however in your case the delimiter for the last item is not ' ' as you assume
一个 csv 文件就像任何其他文件一样是一个字符流。getline 从文件中读取到一个分隔符,但是在您的情况下,最后一项的分隔符不是您假设的 ' '
getline(file, genero, ' ') ;
it is newline \n
它是换行符 \n
so change that line to
所以将该行更改为
getline(file, genero); // \n is default delimiter
回答by Ian Jenkins
Your csv is malformed. The output is not three loopings but just one output. To ensure that this is a single loop, add a counter and increment it with every loop. It should only count to one.
您的 csv 格式错误。输出不是三个循环,而是一个输出。为了确保这是一个单一的循环,添加一个计数器并在每个循环中增加它。它应该只数到一。
This is what your code sees
这是你的代码看到的
0,Filipe,19,M\n1,Maria,20,F\n2,Walter,60,M
Try this
尝试这个
0,Filipe,19,M
1,Maria,20,F
2,Walter,60,M
while(file.good())
{
getline(file, ID, ',');
cout << "ID: " << ID << " " ;
getline(file, nome, ',') ;
cout << "User: " << nome << " " ;
getline(file, idade, ',') ;
cout << "Idade: " << idade << " " ;
getline(file, genero) ; \ diff
cout << "Sexo: " << genero;\diff
}
回答by Freeman Zhang
That because your csv file is in invalid format, maybe the line break in your text file is not the \n or \r
那是因为您的 csv 文件格式无效,所以文本文件中的换行符可能不是 \n 或 \r
and, using c/c++ to parse text is not a good idea. try awk:
并且,使用 c/c++ 来解析文本并不是一个好主意。试试 awk:
$awk -F"," '{print "ID=""\tName=""\tAge=""\tGender="}' 1.csv
ID=0 Name=Filipe Age=19 Gender=M
ID=1 Name=Maria Age=20 Gender=F
ID=2 Name=Walter Age=60 Gender=M