VOOZH about

URL: https://www.geeksforgeeks.org/dsa/c-program-to-append-content-of-one-text-file-to-another/

⇱ C program to append content of one text file to another - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C program to append content of one text file to another

Last Updated : 12 Jul, 2025

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: 

  1. 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.
  2. Explicitly write a newline ("\n") to the destination file to enhance readability.
  3. Write content from source file to destination file.
  4. Display the contents in file2.txt to console (stdout). 
     


Output: 

Below is the output of the above program: 
 

👁 Image


 

Time Complexity: O(N) 
Auxiliary Space Complexity: O(1) 


 

Comment
Article Tags: