istringstream - how to do this?

前端 未结 3 1394
逝去的感伤
逝去的感伤 2021-02-04 20:33

I have a file:

a 0 0
b 1 1
c 3 4
d 5 6

Using istringstream, I need to get a, then b, then c, etc. But I don\'t know how to do it because there

3条回答
  •  离开以前
    2021-02-04 20:40

    ifstream file;
    file.open("file.txt");
    string line;
    
    getline(file,line);
    istringstream iss(line);
    iss >> id;
    
    getline(file,line);
    istringstream iss2(line);
    iss2 >> id;
    
    getline(file,line);
    iss.str(line);
    iss >> id;
    

    istringstream copies the string that you give it. It can't see changes to line. Either construct a new string stream, or force it to take a new copy of the string.

提交回复
热议问题