一、描述
如果字符流最后没有一个空白行,那么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; }
代码在linux上,g++ 4.1.2 上显示有问题
时间: 2024-12-18 23:14:08