65,211
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
#include <fstream>
using namespace std;
ifstream& open_file(ifstream& ,const string);
int main()
{
ifstream ifile;
open_file(ifile,"t.txt");
char ch;
ifstream::pos_type mark = ifile.tellg();
while(ifile.get(ch))
cout << ch << " ";
ifile.seekg(mark); //mark换成 ios_base::beg也不行??
cout << endl;
while(ifile.get(ch))
cout << ch << " ";
ifile.close();
}
ifstream& open_file(ifstream &in,const string fileName)
{
in.close();
in.clear();
in.open(fileName.c_str(),ios::in);
if(!in)
cerr <<"can't open file" <<in<<endl;
return in;
}
应该seekg()函数就可以 ,这里有什么问题啊? seekg(0,ios_base::beg)也不行的
#include <iostream>
using namespace std;
#include <fstream>
using namespace std;
ifstream& open_file(ifstream& ,const string);
int main()
{
ifstream ifile;
open_file(ifile,"t.txt");
char ch;
ifstream::pos_type mark = ifile.tellg();
while (ifile.get(ch)) //这里把EOF置位了
cout << ch << " ";
ifile.clear(); //不清除EOF置位,后面的文件操作直接被跳过去,不会实际执行。
ifile.seekg(mark); //mark换成 ios_base::beg也不行??
cout << endl;
while (ifile.get(ch))
cout << ch << " ";
ifile.close();
}
ifstream& open_file(ifstream &in,const string fileName)
{
in.close();
in.clear();
in.open(fileName.c_str(),ios::in);
if (!in)
cerr <<"can't open file" <<in<<endl;
return in;
}