问题描述
我有一个代码,用于读取文件的浮动数字在行存储像这样:3.34 | 2.3409 | 1.0001 | ... | 1.1 |。我想使用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++;
}
在标记点,第一次将行复制到单独的流。在下一次迭代它不工作,它什么也不做。我希望可以使用更多的时间,而不是在每次迭代中构造新的istringstream对象。
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.
推荐答案
istringstream ...
After setting the row into the istringstream...
separate.str(row);
...通过调用重置
separate.clear();
这将清除在上一次迭代中设置的任何iostate标志或通过设置字符串。
http://www.cplusplus.com/reference/iostream/ios/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/
这篇关于C ++ - 重复使用istringstream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!