我有一个stringstream
我正在阅读的实例.在从流中获取数据的某个点上,我需要读取可能存在或不存在的标识符.逻辑是这样的:
std::string identifier; sstr >> identifier; if( identifier == "SomeKeyword" ) //process the the rest of the string stream using method 1 else // back up to before we tried to read "identifier" and process the stream using method 2
我怎样才能实现上述逻辑?
使用流tellg()
和seekg()
方法,例如:
std::string identifier; std::stringstream::pos_type pos = sstr.tellg(); sstr >> identifier; if (identifier == "SomeKeyword") { //process the rest of the string stream using method 1 } else { // back up to before we tried to read "identifier sstr.seekg(pos); // process the stream using method 2 }