C ++程序将一个文本文件的内容附加到另一个

这是一个C ++程序,用于将一个文本文件的内容附加到另一个。

输入项

a.txt file contains “NH”
a1.txt file contains “ooo”

输出结果

Nhooo

算法

Begin
    将fstream类对象定义为fin。
    用输入文件流类对象fin打开一个输入文件a.txt。
    打开输出文件流类对象fout的输出文件a1.txt
    在附加模式下。
    然后检查文件是否不存在
    打印“找不到文件”。
    否则从fin到fout追加内容。
    以读取模式打开目标文件。
    将其内容显示为输出。 
End.

范例程式码

#include <bits/stdc++.h>
#include<fstream>
using namespace std;
int main() {
   fstream f;
   ifstream fin;
      fin.open("a.txt");
      ofstream fout;
      fout.open("a1.txt", ios::app);
   if (!fin.is_open()) {
      cout << "Nhooo";
   } else {
      fout << fin.rdbuf();
   }
   string word;
   f.open("a1.txt");
   while (f >> word) {
      cout << word << " ";
   }
   return 0;
}

输出结果

Nhooo