这些线程不回答我:
How do you clear a stringstream variable?
std::ifstream file( szFIleName_p ); if( !file ) return false; // create a string stream for parsing std::stringstream szBuffer; std::string szLine; // current line std::string szKeyWord; // first word on the line identifying what data it contains while( !file.eof()){ // read line by line std::getline(file,szLine); // ignore empty lines if(szLine == "") continue; szBuffer.str(""); szBuffer.str(szLine); szBuffer>>szKeyWord;
szKeyword将始终包含第一个单词,szBuffer没有被重置,在任何关于如何使用stringstream的任何地方找不到一个明确的例子.
新代码回答:
... szBuffer.str(szLine); szBuffer.clear(); szBuffer>>szKeyWord; ...
好的,这是我的最终版本:
std::string szLine; // current line std::string szKeyWord; // first word on the line identifying what data it contains // read line by line while( std::getline(file,szLine) ){ // ignore empty lines if(szLine == "") continue; // create a string stream for parsing std::istringstream szBuffer(szLine); szBuffer>>szKeyWord;@H_404_18@