c – 如何重用stringstream
发布时间:2020-12-16 03:09:08 所属栏目:百科 来源:网络整理
导读:这些线程不回答我: resetting a stringstream 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; // cu
这些线程不回答我:
resetting a stringstream 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; 解决方法
调用str(“”)后,您没有清除()流.再看看
this answer,它也解释了为什么你应该使用str(std :: string())重置.而在您的情况下,您也可以仅使用str(szLine)重置内容.
如果不调用clear(),流的标志(如eof)不会被重置,从而导致令人惊讶的行为;) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |