C++文件读取如何跳过前3行,注:前三行的字符不一样。长短不一。如何跳过?

哪个大神能给我个代码 ?

#include <iostream>
#include <fstream>
using namespace std;

int main() {
ifstream file("file.txt");
for (int i = 0; i < 3;) {
if (file.get() == '\n') i++;
}
// 到这里就能跳过前3行
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-06-27
你就直接读,读一行,再读一行,再读一行,然后读到的就是第四行了,忽略了前三行。追问

怎么把前三行读的时候忽略,你给我个关键代码

追答

string filename;
cin>>filename;
ifstream infile(filename.c_str());
string temp;
getline(infile,temp); //读文件一行
getline(infile,temp);
getline(infile,temp);
然后再读你要的数据即可

追问

这个函数参数错了?能不能告诉我一下,我急用呢,不然给我QQ号,我加你,给我解答一下好不好

追答

解决了吧?楼下两位都说了。你的myfile声明错了,读文件要声明为ifstream myfile;你写成
ofstream myfile;了。我QQ1259745316

本回答被提问者采纳
第2个回答  2014-06-27
getline(myfile,tem,'\n'); .//加上结束标记
相似回答