#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
const string filename="F:/1.txt";
int main()
{
ifstream fin;
fin.open(filename.c_str(),ios_base::in); //以读取方式关联流和文件
if(fin.is_open()){
cout<<filename<<endl;
cout<<fin.rdbuf(); //输出文件内容
cout<<endl;
}
fin.clear();
fin.close();
ofstream fout;
fout.open(filename.c_str(),ios_base::out|ios_base::app); //追加方式
if(!fout.is_open()){ //关联失败
cerr<<"Can't open "<<filename<<" file for output.\n";
exit(EXIT_FAILURE);
}
cout<<"Enter new file contents (to be append):"<<endl;
string buffer;
while(getline(cin,buffer)&&buffer.size()>0){
fout<<buffer<<endl;
}
fout.clear();
fout.close();
fin.open(filename.c_str(),ios_base::in); //再次打开这个文件
if(fin.is_open()){
cout<<filename<<endl;
cout<<fin.rdbuf();
cout<<endl;
}
fin.clear();
fin.close();
return 0;
}
C++ STL 文件内容的显示和追加
最新推荐文章于 2024-05-04 17:15:47 发布