C++ program to append content of one text file to another



This is a C++ program to append the content of one text file to another.

Input

a.txt file contains “Tutorials”
a1.txt file contains “point”

Output

Tutorialspoint

Algorithm

Begin
   Define a fstream class object as fin.
   Open a input file a.txt with input file stream class object fin.
   Open a output file a1.txt with output file stream class object fout
   in append mode.
   Check if the file is not existing then
      Print “File not found”.
   Else append content from fin to fout.
   Open the destination file in read mode.
   Display its content as output. 
End.

Example Code

 Live Demo

#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 << "Tutorialspoint";
   } else {
      fout << fin.rdbuf();
   }
   string word;
   f.open("a1.txt");
   while (f >> word) {
      cout << word << " ";
   }
   return 0;
}

Output

Tutorialspoint
Samual Sam
Samual Sam

Learning faster. Every day.


Advertisements