一个文件追加到另一个文件的末尾的C++程序

来源:百度知道 编辑:UC知道 时间:2024/06/26 02:20:27
写一个程序,连接两个文件,即将一个文件追加到另一个文件的末尾
高手赶快 急用!!!
能详细点么

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

int main()
{

string str;
ifstream finput;
finput.open("1.txt");
if (finput.fail())
{
cout<<"open file error!\n";
exit(0);
}
ofstream fout;
fout.open("a.txt",ios::app);
if (fout.fail())
{
cout<<"open file error!\n";
exit(0);
}

while (finput>>str)
{
fout<<str<<" ";
}

finput.close();
fout.close();

return 0;
}

用ofstream函数按追加方式打开文件
然后逐个储存吧
----------------以下内容供参考
在fstream类中,有一个成员函数open(),就是用来打开文件的,其原型是:

void open(const char* filename,int mode,int access);

参数:

filename: 要打开的文件名
mode: 要打开文件的方式
access: 打开文件的属性
打开文件的方式在类ios(是所有流式I/O类的基类)中定义,常用的值