久久久久久久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 ()?環形?)
主站蜘蛛池模板: 国产乱码精品一区二区三区忘忧草 | 亚洲黄色视屏 | 91精品国产综合久久福利软件 | 日日噜噜噜夜夜爽爽狠狠视频97 | 日韩免费三级 | 久久成人精品视频 | 在线播放中文字幕 | 中文天堂在线观看 | 精品视频在线观看 | 国产欧美一区二区三区在线播放 | 精品在线观看一区 | 一二三四在线视频观看社区 | 日韩一二三区 | 日韩欧美在线观看视频 | 黄色欧美大片 | 午夜小电影 | 成人一级视频在线观看 | 中文在线a在线 | 国产av毛片 | av毛片 | 国产精品美女久久久久aⅴ国产馆 | 国产91久久精品一区二区 | 成年人黄色免费视频 | 99精品国产一区二区青青牛奶 | 成人在线观看欧美 | 亚洲精品视频一区二区三区 | 亚洲欧美中文日韩在线v日本 | 国产在线中文字幕 | 成人二区 | 色综合视频 | 最新日韩欧美 | 国产日韩一区二区 | 亚洲国产精品va在线看黑人 | 奇米久久 | 亚洲巨乳自拍在线视频 | 国产视频福利一区 | 欧美日韩精品一区 | 奇米av | www国产成人免费观看视频,深夜成人网 | 久久国产精品色av免费观看 | 欧美日本韩国一区二区三区 |