爲什麼ss >> aa >> bb >> cc >> dd
可用於狀態檢查?如果我使用ss >> aa >> bb >> cc >> dd >> ee
這個操作的返回值是什麼?在條件下使用提取算子的返回值?
ifstream inputFile("source.txt", ifstream::in);
string aa, bb, cc, dd;
char line[1024];
while(!inputFile.eof())
{
inputFile.getline(line, 1023);
stringstream ss(stringstream::in | stringstream::out);
ss.str(line);
if(ss >> aa >> bb >> cc >> dd)
{
cout << aa << "-" << bb << "-" << cc << "-" << dd << endl;
}
}
隨着的Source.txt這樣的:
1aaa ddd eee asd
2dfs dfsf sdfs fd
3sdf sdfsdfsdf d s
請注意,您在使用inputFile.getline'的'結果沒有檢查它是否成功與否。 – 2013-03-04 13:53:28