C++ C++;-重复使用istringstream
我有一个代码,用于读取行中存储有浮点数的文件,如下所示:“3.34 | 2.3409 | 1.0001 |……| 1.1 |”。我想使用istringstream阅读它们,但它并不像我预期的那样工作:C++ C++;-重复使用istringstream,c++,string,split,istringstream,C++,String,Split,Istringstream,我有一个代码,用于读取行中存储有浮点数的文件,如下所示:“3.34 | 2.3409 | 1.0001 |……| 1.1 |”。我想使用istringstream阅读它们,但它并不像我预期的那样工作: string row; string strNum; istringstream separate; // textovy stream pro konverzi while ( getline(file,row) ) { separate.str(row);
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对象的情况下,它可以被使用更多次。将行设置到istringstream中后
separate.str(row);
。。。通过调用重置它
separate.clear();
这将清除在上一次迭代中或通过设置字符串设置的任何iostate标志。
您需要添加一个
separate.clear()
separate.str(row)后的行以清除状态位,否则设置eofbit
,后续读取失败。非常感谢。这是我在许多代码中丢失的非常重要的信息;)但它真的需要在后面吗?我想你也可以先把它清理干净。