1

I have a file which contains three integers per line. When I read the line I use a stringstream to separate the values, but it only reads the first value as it is. The other two are read as zero's.

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*
    }
}

In the case above, id is extracted correctly, but income, and members get assigned zero instead of the actual value.

EDIT: Solved

Never mind. The code works correctly. The error was in my print statement. I had a for loop printing the array at the same index every time.

4

1 回答 1

1

为什么不直接从文件中读取?

while( inputstream ) {
    if( ! inputstream >> id ) ...
    if( ! inputstream >> income ) ...
    if( ! inputstream >> members ) ...
}
于 2010-05-08T21:09:13.153 回答