C++標準ライブラリ文字ストリーム処理時getlineのピット
一、説明
文字ストリームに最後に空白の行がない場合、getlineが最後の行のデータを処理するとseekgは失効します.
二、コードテスト
コードはlinux上、g++4.1である.2に問題が表示されます
文字ストリームに最後に空白の行がない場合、getlineが最後の行のデータを処理するとseekgは失効します.
二、コードテスト
#include <sstream>
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
const char* cs = "line1
line2
line3";
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に問題が表示されます