Given a source file and a destination file, we can append the contents of the source file to the end of the destination file. C++ provides file-stream classes and C-style file functions to perform this operation.
- The source file is opened in read mode to access its contents.
- The destination file is opened in append mode so its existing content is preserved.
Example:
Consider two text files with the following contents:
file.txt : geeks
file2.txt : geeks forAfter appending the contents of file.txt to file2.txt, the destination file becomes:
file2.txt : geeks for geeks
Approaches to Append One File to Another
The following approaches can be used to append the contents of one text file to another:
1. Using C++ File Streams
The ifstream class can be used to read the source file, while ofstream can open the destination file in append mode using ios::app.
Steps
- Open the source file using ifstream.
- Open the destination file using ofstream with ios::app.
- Copy the contents of the source file to the destination file.
- Open the destination file in read mode and display its contents.
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ifstream source("file.txt");
ofstream destination("file2.txt", ios::app);
if (!source || !destination) {
cout << "Unable to open file";
return 1;
}
destination << source.rdbuf();
source.close();
destination.close();
ifstream file("file2.txt");
string content;
while (getline(file, content))
cout << content << endl;
file.close();
return 0;
}
output
geeks for geeksExplanation: ifstream opens file.txt for reading, while ofstream opens file2.txt in append mode andrdbuf() copies the source content to the destination, which is then opened and displayed.
2. Using C-Style File Functions
The C standard library provides functions such as fopen(), fgetc(), fprintf(), and fclose() that can also be used in a C++ program to work with files.
Steps:
- Open the source file in read mode using fopen().
- Open the destination file in append mode using fopen().
- Read each character from the source file using fgetc().
- Write each character to the destination file using fprintf().
- Close both files using fclose().
- Open the destination file in read mode and display its contents.
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
FILE* source = fopen("file.txt", "r");
FILE* destination = fopen("file2.txt", "a");
if (source == nullptr || destination == nullptr) {
cout << "Unable to open file";
if (source)
fclose(source);
if (destination)
fclose(destination);
return 1;
}
int ch;
while ((ch = fgetc(source)) != EOF)
fprintf(destination, "%c", ch);
fclose(source);
fclose(destination);
FILE* file = fopen("file2.txt", "r");
if (file == nullptr) {
cout << "Unable to open destination file";
return 1;
}
while ((ch = fgetc(file)) != EOF)
printf("%c", ch);
fclose(file);
return 0;
}
Output
geeks for geeksExplanation
- fopen() opens the source file in read mode and the destination file in append mode, while fgetc() reads the source file character by character.
- fprintf() appends each character to the destination file, which is then reopened for display, and fclose() closes the opened file streams.
File Opening Modes Used
| Mode | Purpose |
|---|---|
| "r" | Opens an existing file for reading. |
| "a" | Opens a file for appending. Creates the file if it does not exist. |
| ios::app | Opens a C++ output stream in append mode. |