
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Append Content of One Text File to Another in C++
In C++, we can work with files to read, write, or even combine(append) their contents. Here, our task is to append the content of one text file to another.
Imagine a scenario where we have two txt files which is named as 'source.txt' and 'destination.txt' files. So, with the help of these files we need to copy everything from the source file and append it at the end of the destination file.
Example
Here, we are giving an input as two text files as source.txt and destination.txt to append content in C++:
source.txt file contains "Tutorials" destination.txt file contains "point"
After appending the two files with each other, the output will be:
Tutorialspoint
Algorithm
Following is the algorithm is as follows:
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.
To achieve this, below are the different approaches we can use in C++:
Appending with fstream and open()
In this approach, we use fstream where the destination file is opened in 'ios::app' mode, allowing content to be appended directly.
Syntax
Following is the syntax is as follows:
fstream file; file.open("destination.txt", ios::app); file<<data;
Example
This program reads each line from "source.txt" and appends (adds) it to the end of "destination.txt".
#include<iostream> #include<fstream> #include<string> using namespace std; int main() { fstream source("source.txt", ios::in); fstream dest("destination.txt", ios::app); if (!source || !dest) { cerr<<"Error opening files!"<<endl; return 1; } string line; while (getline(source, line)) { dest<<line<<endl; } source.close(); dest.close(); cout<<"Content appended successfully."<<endl; return 0; }
If the file is opened, the output will be:
Content appended successfully.
If the file is not opened, the output will be:
Error opening files!
Manual File I/O with ifstream & ofstream
This approach uses 'ifstream' for reading from the source and 'ofstream' to append for writing to the destination.
Syntax
Following is the syntax is as follows:
ifstream source("source.txt"); ofstream dest("destination.txt", ios::app); while (getline(source, line)) dest<<line<<endl;
Example
This program reads each line from source.txt and adds it to the end of destination.txt without deleting its existing content.
#include<iostream> #include<fstream> #include<string> using namespace std; int main() { ifstream src("source.txt"); ofstream dest("destination.txt", ios::app); if (!src.is_open() || !dest.is_open()) { cerr<<"Unable to open one or both files."<<endl; return 1; } string line; while (getline(src, line)) { dest<<line<<endl; } src.close(); dest.close(); cout<<"Data has been appended to destination.txt"<<endl; return 0; }
If the file is opened, the output will be:
Data has been appended to destination.txt
If the file is not opened, the output will be:
Unable to open one or both files.
Appending Using File Buffers (rdbuf)
The rdbuf() is used to copy the entire buffers from the source file into the destination file.
Syntax
Following is the syntax is as follows:
ifstream source("source.txt", ios::binary); ofstream dest("destination.txt", ios::binary | ios::app); dest<<source.rdbuf();
Example
This program appends the entire content of source.txt to destination.txt in binary mode using a buffer.
#include<iostream> #include<fstream> using namespace std; int main() { ifstream src("source.txt", ios::binary); ofstream dest("destination.txt", ios::binary | ios::app); if (!src || !dest) { cerr<<"Failed to open files."<<endl; return 1; } dest<<src.rdbuf(); src.close(); dest.close(); cout<<"File content appended using rdbuf."<<endl; return 0; }
If the file is opened, the output will be:
File content appended using rdbuf.
If the file is not opened, the output will be:
Failed to open files.