【发布时间】:2014-01-14 18:54:30
【问题描述】:
我正在通过这样的函数读取文件:
#include <iostream>
#include <fstream>
#include <string>
...
void readfile(string name){
string line;
int p = 0;
ifstream f(name.c_str());
while(getline(f,line)){
p++;
}
f.seekg(0);
cout << p << endl;
getline(f,line);
cout << line << endl;
}
Mi 文件有 3 行:
first
second
third
我期望的输出:
3
first
相反,我得到:
3
(nothing)
为什么我的 seekg 不起作用?
【问题讨论】:
-
如果在使用
seekg之前执行f.clear()会发生什么? -
@sftrabbit ...会发生什么?它有效...谢谢,速度很快。
-
请注意,自 C++11 起,
seekg本身会清除eofbit。