c++基本文件操作

#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>

using namespace std;

int main()
{
ifstream in_file;
ofstream out_file ;

string out_string;

out_file.open("/home/merlin/code/word.txt");
if(out_file.is_open())
{
cout<<"enter words!"<<endl;
while( getline(cin,out_string) )
out_file<<out_string<<endl;
}
else
{
cout<<"open file fail!!"<<endl;
exit(EXIT_FAILURE);
}
out_file.close();

in_file.open("/home/merlin/code/word.txt");
while( in_file.good() )
{
cout<< (char) in_file.get();
}
in_file.close();

return 0;
}

你可能感兴趣的:(C++ 文件操作)