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

16 Jun, 2022
Given source and destination text files. Append the content from the source file to the destination file and then display the content of the destination file.Example :
Input : file.txt : "geeks", file2.txt : "geeks for"
Output: file2.txt : "geeks for geeks"
Method 1:Approach:
Open file.txt in inputstream and file2.txt in outputstream with the append option, so that the previous content of the file are not deleted.Check if there’s an error in opening or locating a file. If yes, then throw an error message.If both files are found, then write content from the source file to the destination file.Display the content of the destination file.
Open file.txt in inputstream and file2.txt in outputstream with the append option, so that the previous content of the file are not deleted.
Check if there’s an error in opening or locating a file. If yes, then throw an error message.
If both files are found, then write content from the source file to the destination file.
Display the content of the destination file.
Below is the implementation of the above approach:
CPP
// C++ implementation to append// content from source file to// destination file#include <bits/stdc++.h>using namespace std; // driver codeint main(){    fstream file;     // Input stream class to    // operate on files.    ifstream ifile("file.txt", ios::in);     // Output stream class to    // operate on files.    ofstream ofile("file2.txt", ios::out | ios::app);     // check if file exists    if (!ifile.is_open()) {         // file not found (i.e, not opened).        // Print an error message.        cout << "file not found";    }    else {        // then add more lines to        // the file if need be        ofile << ifile.rdbuf();    }    string word;     // opening file    file.open("file2.txt");     // extracting words form the file    while (file >> word) {         // displaying content of        // destination file        cout << word << " ";    }     return 0;}
file not found
Method 2: We can do the same using different functions mentioned below:
fopen(): Returns a pointer to the object that controls the opened file stream
fprintf(): Writes the string pointed by format to the stream
fclose(): Closes the file associated with the stream and disassociates it.
fgetc(): Returns the character currently pointed by the internal file position indicator of the specified stream
Approach:
Open source file in read mode and destination file in append mode using fopen()check if they existIterate every character of the source file using fgetc() and print it to the destination file using fprintf() with while loopClose both files using fclose()Open destination file in read modeprint itclose it
Open source file in read mode and destination file in append mode using fopen()
check if they exist
Iterate every character of the source file using fgetc() and print it to the destination file using fprintf() with while loop
Close both files using fclose()
Open destination file in read mode
print it
close it
Below is the implementation of the above approach:
C++
// C++ program to Append and Read text of text files#include <bits/stdc++.h> using namespace std;int main(){    // open file in read mode to read it's content    FILE* file = fopen("file.txt", "r");       // open file in append mode to append readed content    FILE* file2 = fopen("file2.txt", "a");    if (file2 == NULL) {        cout << "file not found";        return 1;    }    else if (file == NULL) {        cout << "file not found";        return 1;    }       // Read contents from file    char ch = fgetc(file);    while (ch != EOF) {               // print readed content from file in file2        fprintf(file2, "%c", ch);        ch = fgetc(file);    }    fclose(file);    fclose(file2);       // open file 2 again in read mode to read the content    FILE* file3 = fopen("file2.txt", "r");       // Read contents from file    char ch2 = fgetc(file3);    while (ch2 != EOF) {               // print readed content from file        printf("%c", ch2);        ch2 = fgetc(file3);    }    fclose(file3);} // This code is contributed by Shivesh Kumar Dwivedi
file not found
cpdwivedi916
cpp-file-handling
C++
C++ Programs
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Sorting a vector in C++
Polymorphism in C++
Friend class and function in C++
std::string class in C++
Pair in C++ Standard Template Library (STL)
Header files in C/C++ and its uses
Sorting a Map by value in C++ STL
Program to print ASCII Value of a character
How to return multiple values from a function in C or C++?
Shallow Copy and Deep Copy in C++