C Program To Merge Contents Of Two Files Into A Third File

C Program To Merge Contents Of Two Files Into A Third File

The given steps outline a simple process for merging the contents of two files, file1.txt and file2.txt, into a new file named file3.txt.

  1. Open file1.txt and file2.txt in read mode.
  2. Open file3.txt in write mode.
  3. Copy characters from file1.txt to file3.txt in a loop.
  4. Copy characters from file2.txt to file3.txt in a loop.
  5. Close all files.

These steps describe a basic file merging operation where the contents of file1.txt and file2.txt are sequentially copied to a new file, file3.txt. This process can be implemented in various programming languages, such as C, C++, Python, etc.

Inorder to run the below program successfully, we have to include the file1.txt and fil2.txt must exits in the same folder



#include <stdio.h> 
#include <stdlib.h> 
  
int main() 
{ 
   // Open two files to be merged 
   FILE *fp1 = fopen("file1.txt", "r"); 
   FILE *fp2 = fopen("file2.txt", "r"); 
  
   // Open file to store the result 
   FILE *fp3 = fopen("file3.txt", "w"); 
   char c; 
  
   if (fp1 == NULL || fp2 == NULL || fp3 == NULL) 
   { 
         puts("Could not open files"); 
         exit(0); 
   } 
  
   // Copy contents of first file to file3.txt 
   while ((c = fgetc(fp1)) != EOF) 
      fputc(c, fp3); 
  // Copy contents of second file to file3.txt 
   while ((c = fgetc(fp2)) != EOF) 
      fputc(c, fp3); 
  
   printf("Merged file1.txt and file2.txt into file3.txt"); 
  
   fclose(fp1); 
   fclose(fp2); 
   fclose(fp3); 
   return 0; 
} 

Output

Merged file1.txt and file2.txt into file3.txt

FAQ- C Program To Merge Contents Of Two Files Into A Third File

Q1. How to add contents of one file to another in C?

Ans.
Open file1.txt and file2.txt in read mode.
Open file3.txt in write mode.
Copy characters from file1.txt to file3.txt with a newline for readability.
Copy characters from file2.txt to file3.txt with a newline.
Display the contents of file2.txt to the console (stdout).
Close all files.
This process involves merging the contents of file1.txt and file2.txt into a new file file3.txt while enhancing readability by explicitly writing newline characters between the content from each source file. Additionally, it displays the contents of file2.txt to the console.

Q2. How can I combine or concatenate contents of two files and store into a third file in Linux?

Ans. The cat command in Linux is a handy tool for files. It shows what’s inside a file (cat filename), helps make a new file (cat > newfile), adds stuff to an existing file (cat >> existingfile), and combines info from different files (cat file1 file2 > combinedfile). It’s a useful and common command in Linux.

Q3.Can you include a .C file into another .C file?

Ans. Yes, it is possible to include a C file in another C file,

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment