簡體   English   中英

C ++字符串流讀取全零

[英]C++ stringstream reads all zero's

我有一個文件,每行包含三個整數。 當我讀取該行時,我使用stringstream來分隔值,但是它僅按原樣讀取第一個值。 其他兩個讀為零。

ifstream inputstream(filename.c_str());
if( inputstream.is_open() ){

    string line;
    stringstream ss;

    while( getline(inputstream, line) ){
        //check line and extract elements
        int id;
        double income;
        int members;

        ss.clear();
        ss.str(line);
        ss >> id >> income >> members;*emphasized text*
    }
}

在上述情況下,id可以正確提取,但是收入和成員分配的零而不是實際值。

編輯:解決

沒關系。 該代碼正常工作。 錯誤出在我的打印聲明中。 我有一個for循環,每次都在相同的索引處打印數組。

為什么不直接從文件中讀取?

while( inputstream ) {
    if( ! inputstream >> id ) ...
    if( ! inputstream >> income ) ...
    if( ! inputstream >> members ) ...
}

暫無
暫無

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

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