在下面的c ++代码中,通过使用getline()对它进行一些读取操作后,我无法将读取指针退回到第一个字符串:
std::string token;
std::string first;
std:string str = "1,2,3,4,5";
std::istringstream range(str);
while(getline(range,token,','))
cout<<"token="<<token<<endl;
getling(range,first,',');
cout<<first;
您正在消耗所有流,并在流的末尾while
设置其eof
位。您需要clear
先到流,然后再回到第一个位置:
#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::string token;
std::string first;
std::string str = "1,2,3,4,5";
std::istringstream range(str);
while (std::getline(range, token, ','))
std::cout << "token=" << token << std::endl;
// need these 2 lines
range.clear(); // clear the `failbit` and `eofbit`
range.seekg(0); // rewind
std::getline(range, first, ',');
std::cout << first;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句