C program to append content of one text file to another Last Updated : 12 Jul, 2025 Comments Improve Suggest changes 3 Likes Like Report Pre-requisite: File Handling in C Given the source and destination text files, the task is to append the content from source file to destination file and then display the content of the destination file.Examples: Input: file1.text This is line one in file1 Hello World. file2.text This is line one in file2 Programming is fun. Output: This is line one in file2 Programming is fun. This is line one in file1 Hello World. Approach: Open file1.txt and file2.txt with "a+"(append and read) option, so that the previous content of the file is not deleted. If files don't exist, they will be created.Explicitly write a newline ("\n") to the destination file to enhance readability.Write content from source file to destination file.Display the contents in file2.txt to console (stdout). C // C program to append the contents of // source file to the destination file // including header files #include <stdio.h> // Function that appends the contents void appendFiles(char source[], char destination[]) { // declaring file pointers FILE *fp1, *fp2; // opening files fp1 = fopen(source, "a+"); fp2 = fopen(destination, "a+"); // If file is not found then return. if (!fp1 && !fp2) { printf("Unable to open/" "detect file(s)\n"); return; } char buf[100]; // explicitly writing "\n" // to the destination file // so to enhance readability. fprintf(fp2, "\n"); // writing the contents of // source file to destination file. while (!feof(fp1)) { fgets(buf, sizeof(buf), fp1); fprintf(fp2, "%s", buf); } rewind(fp2); // printing contents of // destination file to stdout. while (!feof(fp2)) { fgets(buf, sizeof(buf), fp2); printf("%s", buf); } } // Driver Code int main() { char source[] = "file1.txt", destination[] = "file2.txt"; // calling Function with file names. appendFiles(source, destination); return 0; } Output: Below is the output of the above program: Time Complexity: O(N) Auxiliary Space Complexity: O(1) Comment Y yashbeersingh42 Follow 3 Improve Y yashbeersingh42 Follow 3 Improve Article Tags : C Programs DSA Arrays C-File Handling Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 2 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 15 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 6 min read Problem of The Day - Develop the Habit of Coding 5 min read Like