#include<fstream>
#include<string>
#include<iostream>
using namespace std;
ofstream & openFile(ofstream &out,const string &fileName)
{
//
out.close();
out.clear();
out.open(fileName.c_str());
return out;
}
ifstream & openFile(ifstream &in,const string &fileName)
{
//
in.close();
in.clear();
in.open(fileName.c_str());
return in;
}
ostream & write(ostream &out)
{
//
int ival;
cout<<"
Enter some integers:"<<endl;
while(cin>>ival)
out<<ival<<endl; // ,
return out;
}
istream & read(istream &in)
{
//
int data;
cout<<"
The data in disk file is:"<<endl;
while(in>>data)
cout<<data<<endl; // ,
return in;
}
int main()
{
string fileName;
cout<<"
Enter file name:"<<endl;
cin>>fileName;
//
ofstream outFile;
openFile(outFile,fileName);
write(outFile);
//
ifstream inFile;
openFile(inFile,fileName);
read(inFile);
inFile.close();
return 0;
}
zhaobin@debian:~$ vim x.cc
zhaobin@debian:~$ g++ -o x x.cc
zhaobin@debian:~$ ./x
Enter file name:
yyy
Enter some integers:
9 3 66 1
The data in disk file is:
9
3
66
1