Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

string - C++ - repeatedly using istringstream

I have a code for reading files with float numbers on line stored like this: "3.34|2.3409|1.0001|...|1.1|". I would like to read them using istringstream, but it doesn't work as I would expect:

  string row;
  string strNum;

  istringstream separate;  // textovy stream pro konverzi

   while ( getline(file,row) ) {
      separate.str(row);  // = HERE is PROBLEM =
      while( getline(separate, strNum, '|') )  { // using delimiter
        flNum = strToFl(strNum);    // my conversion
        insertIntoMatrix(i,j,flNum);  // some function
        j++;
      }
      i++;
    }

In marked point, row is copied into separate stream only first time. In next iteration it doesn't work and it does nothing. I expected it is possible to be used more times without constructing new istringstream object in every iteration.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

After setting the row into the istringstream...

separate.str(row);

... reset it by calling

separate.clear();

This clears any iostate flags that are set in the previous iteration or by setting the string. http://www.cplusplus.com/reference/iostream/ios/clear/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...