我正在通过这样的函数读取文件:
#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 不起作用?