簡體   English   中英

從格式化的.txt文件讀取C ++

[英]C++ read from formatted .txt file

我需要從逗號分隔的.txt文件中讀取內容,其中每行看起來都是這樣的:

1234,0987,Bob,23.45

(即int,int,string,double)

使用以下安裝代碼:

fstream myFile;
myFile.open("textfile.txt" , ios::in);
if (myFile.is_open()) {
    //read in characters as appropriate type until ','
}

我嘗試使用

myFile >> int1 ......

但是我不確定如何處理逗號。 當讀取整數時,它們可能會被過濾掉,但是當我到達字符串時,這行得通嗎?

我的一位同學建議使用stringstream,但是我發現cplusplus.com上的文檔很麻煩。

您可能想嘗試一下std::getline函數:

istream& getline ( istream& is, string& str, char delim );
istream& getline ( istream& is, string& str );

(來自http://www.cplusplus.com/reference/string/getline/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM