C++ program to append content of one text file to another Last Updated : 23 Nov, 2022 Comments Improve Suggest changes Like Article Like Report 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. 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 code int 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; } Outputfile 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 streamfprintf(): Writes the string pointed by format to the streamfclose(): 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 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 read 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 read 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 read content from file printf("%c", ch2); ch2 = fgetc(file3); } fclose(file3); } // This code is contributed by Shivesh Kumar Dwivedi Outputfile not found Comment More infoAdvertise with us Next Article C++ program to append content of one text file to another N nickhilrawat Follow Improve Article Tags : Misc C++ Programs C++ DSA cpp-file-handling +1 More Practice Tags : CPPMisc Similar Reads C++ Program to Copy the Contents of One File Into Another File Here, we will see how to develop a C++ program to copy the contents of one file into another file. Given a text file, extract contents from it and copy the contents into another new file. After this, display the contents of the new file. Approach:Open the first file which contains data. For example, 2 min read C++ Program to Copy One File into Another File To copy the text/contents of one file to another file, we should know the basics of reading and writing a text file in C++. To copy the file using C++, we read the contents of the source file and write it into the destination file. Steps to copy one file to another in C++: Create objects of ifstream 2 min read C++ Program to Read Content From One File and Write it Into Another File Here, we will see how to read contents from one file and write it to another file using a C++ program. Let us consider two files file1.txt and file2.txt. We are going to read the content of file.txt and write it in file2.txt Contents of file1.txt: Welcome to GeeksForGeeks Approach: Create an input f 2 min read C++ Program to Append a String in an Existing File Here, we will build a C++ program to append a string in an existing file using 2 approaches i.e. Using ofstreamUsing fstreamC++ programming language offers a library called fstream consisting of different kinds of classes to handle the files while working on them. The classes present in fstream are 2 min read C++ Program to Count the Number of Spaces in a File Here, we will see how to count number of spaces in a given file. First, we will read the contents of the file word by word, keep one counter variable 'count', and set it to zero while declaring. Increment 'count' each time you read a single word from the file. Example: Input: Geeks For Geeks Output: 2 min read Like