久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

c++ 從.csv文件中讀取

c++ Read from .csv file(c++ 從.csv文件中讀取)
本文介紹了c++ 從.csv文件中讀取的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有這個代碼,它應該在控制臺中輸出 .csv 文件中的信息;

I have this code which is supposed to cout in console the information from the .csv file;

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<< " "  ;

}

還有一個包含這個的 csv 文件(當我用記事本打開時):

And a csv file that has this (when I open with notepad):

0,Filipe,19,M

1,Maria,20,F

2,Walter,60,M

每當我運行程序時,控制臺都會顯示:

Whenever I run the program the console will display this:

我的問題是為什么程序不在每一行而不是只在第一行重復這些 cout 消息

My question is why isn't the program repeating those cout messages in every line instead of only in the first one

順便說一句,nome 是名字,idade 是年齡,genero/sexo 是性別,發帖前忘記翻譯了

Btw , nome is name, idade is age, and genero/sexo is gender, forgot to translate before creating this post

推薦答案

你可以關注這個答案 查看在 C++ 中處理 CSV 的許多不同方法.

You can follow this answer to see many different ways to process CSV in C++.

在您的情況下,對 getline 的最后一次調用實際上是將第一行的最后一個字段和所有剩余的行放入變量 genero 中.這是因為在文件末尾之前沒有找到空格分隔符.嘗試將空格字符改為換行符:

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(file, genero, file.widen('
'));

或更簡潔:

    getline(file, genero);

此外,您對 file.good() 的檢查還為時過早.文件中的最后一個換行符仍然在輸入流中,直到它被下一次 getline() 調用 ID 丟棄.正是在這一點上檢測到文件末尾,因此檢查應以此為基礎.您可以通過將 while 測試更改為基于對 ID 本身的 getline() 調用來解決此問題(假設每一行格式正確).

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

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<< " "  ;
}

為了更好地檢查錯誤,您應該檢查每次調用 getline() 的結果.

For better error checking, you should check the result of each call to getline().

這篇關于c++ 從.csv文件中讀取的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 国产精品一区二区电影 | 午夜小视频在线观看 | 中文字幕在线精品 | 国产成人高清在线观看 | 欧美午夜一区 | 成人在线免费观看视频 | 国产 日韩 欧美 在线 | 国产精品久久久久久久久久久久冷 | 伊人久久大香线 | 久久久久久久av | 国产精品久久久久久久久久久久久 | 日韩理论电影在线观看 | 91欧美精品成人综合在线观看 | 粉嫩国产精品一区二区在线观看 | 天天综合久久 | 性一交一乱一透一a级 | 天天躁日日躁xxxxaaaa | 在线观看黄视频 | 欧美视频区| 日韩精品国产精品 | 欧美一级免费看 | 91福利在线导航 | 久久大陆 | 亚洲视频二区 | 日韩超碰| 国产在线精品一区二区三区 | 国产精品成人一区二区三区夜夜夜 | www.日韩高清 | 在线观看免费av片 | 日韩一区二区不卡 | 精品自拍视频 | 五月婷婷婷| 九九国产| 黄色亚洲 | 中文字幕亚洲精品 | 国产精品美女久久久久久不卡 | 亚洲一区二区三区在线播放 | 亚洲第一区久久 | 亚洲精品一区在线观看 | 国产精品久久久久久久7电影 | 欧美日日 |