如果字符流最后没有一个空白行,那么getline处理最后一行的数据时,seekg会失效
#include <sstream> #include <iostream> using namespace std; int main(int argc, char** argv) { const char* cs = "line1\nline2\nline3"; stringstream ss(cs); for (int i = 0; i < 2; ++i) { streamoff pos = ss.tellg(); string s; getline(ss, s); cout << s << endl; cout << "**" << endl; cout << pos << endl; } cout << endl << "------------------------------------------" << endl << endl; for (int i = 0; i < 2; ++i) { streamoff pos = ss.tellg(); string s; getline(ss, s); cout << s << endl; cout << "$$" << endl; cout << pos << endl; ss.seekg(pos, ios::beg); } return 0; }
原文地址:http://blog.csdn.net/kibaamor/article/details/44154711