简体   繁体   English

c++ 从.csv文件中读取

[英]c++ Read from .csv file

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 是性别,在创建这篇文章之前忘记翻译了

You can follow this answer to 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 .在您的情况下,对getline的最后一次调用实际上是将第一行的最后一个字段和所有剩余的行放入变量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(file, genero, file.widen('\n'));

or more succinctly:或更简洁地说:

    getline(file, genero);

In addition, your check for file.good() is premature.此外,您对file.good()检查还为时过早。 The last newline in the file is still in the input stream until it gets discarded by the next getline() call for ID .文件中的最后一个换行符仍在输入流中,直到它被下一次getline()调用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).您可以通过将while测试更改为基于ID本身的getline()调用来解决此问题(假设每行格式正确)。

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()

a csv-file is just like any other file a stream of characters.一个 csv 文件就像任何其他文件一样是一个字符流。 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 getline 从文件中读取到一个分隔符,但是在您的情况下,最后一项的分隔符不是您假设的 ' '

getline(file, genero, ' ') ; 

it is newline \\n它是换行符 \\n

so change that line to所以将该行更改为

getline(file, genero); // \n is default delimiter

Your csv is malformed.您的 csv 格式错误。 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.它应该只数到一。

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


}

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.并且,使用 c/c++ 来解析文本并不是一个好主意。 try awk:试试 awk:

 $awk -F"," '{print "ID="$1"\tName="$2"\tAge="$3"\tGender="$4}' 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM