The Wayback Machine - https://web.archive.org/web/20240531121810/https://www.geeksforgeeks.org/c-program-merge-contents-two-files-third-file/
Open In App

C Program to merge contents of two files into a third file

Last Updated : 07 Jul, 2017
Improve
Improve
Like Article
Like
Save
Share
Report

Let the given two files be file1.txt and file2.txt. The following are steps to merge.
1) Open file1.txt and file2.txt in read mode.
2) Open file3.txt in write mode.
3) Run a loop to one by one copy characters of file1.txt to file3.txt.
4) Run a loop to one by one copy characters of file2.txt to file3.txt.
5) Close all files.

To successfully run the below program file1.txt and fil2.txt must exits in 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

Related Articles :


Previous Article
Next Article

Similar Reads

C program to copy contents of one file to another file
[GFGTABS] C #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; // For exit() int main() { FILE *fptr1, *fptr2; char filename[100]; int c; printf(&quot;Enter the filename to open for reading: &quot;); scanf(&quot;%s&quot;, filename); // Open one file for reading fptr1 = fopen(filename, &quot;r&quot;); if (fptr1 == NULL) { printf(&quot;Cannot open fi
1 min read
Program to copy the contents of one array into another in the reverse order
Given an array, the task is to copy these array elements into another array in reverse array.Examples: Input: array: 1 2 3 4 5 Output: 5 4 3 2 1 Input: array: 10 20 30 40 50 Output: 50 40 30 20 10 Recommended: Please try your approach on {IDE} first, before moving on to the solution.Let len be the length of original array. We copy every element ori
7 min read
C Program to print contents of file
fopen() is used to open and fclose() is used to close a file in C #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; // For exit() int main() { FILE *fptr; char filename[100], c; printf(&quot;Enter the filename to open \n&quot;); scanf(&quot;%s&quot;, filename); // Open file fptr = fopen(filename, &quot;r&quot;); if (fptr == NULL) { printf(&quot;Ca
1 min read
C program to print odd line contents of a File followed by even line content
Pre-requisite: Basics of File Handling in C Given a text file in a directory, the task is to print all the odd line content of the file first then print all the even line content. Examples: Input: file1.txt: Welcome to GeeksforGeeks Output: Odd line contents: Welcome GeeksforGeeks Even line contents: to Input: file1.txt: 1. This is Line1. 2. This i
2 min read
C Program to read contents of Whole File
C programming language supports four pre-defined functions to read contents from a file, defined in stdio.h header file: fgetc()- This function is used to read a single character from the file.fgets()- This function is used to read strings from files.fscanf()- This function is used to read formatted input from a file.fread()- This function is used
6 min read
Write a C program that displays contents of a given file like 'more' utility in Linux
Write a C program that displays contents of given line page by page. Given number of lines to show as 'n' at a time and a file name, the program should first show n lines, then wait for user to hit a key before showing next n lines and so on. We strongly recommend to minimize the browser and try this yourself first. We can open the given file and p
2 min read
Merge operations using STL in C++ | merge(), includes(), set_union(), set_intersection(), set_difference(), ., inplace_merge,
Some of the merge operation classes are provided in C++ STL under the header file "algorithm", which facilitates several merge operations in a easy manner. Some of them are mentioned below. merge(beg1, end1, beg2, end2, beg3) :- This function merges two sorted containers and stores in new container in sorted order (merge sort). It takes 5 arguments
7 min read
C Program to Read and Print All Files From a Zip File
To understand how to write a C program for reading and printing zip files, it's important to know what exactly a zip file is. At its core, a zip file contains one or more files compressed using specific compression algorithms.Including the compressed data of the files, the zip file contains meta and header information about all files inside the zip
6 min read
C program to compare two files and report mismatches
We are given almost two identical files. But some characters are corrupted. We need to find the line number and their position where those corrupted letter exists as well as Total number of corrupted (error) letters. Examples: Input : file1.txt contains it is fun file2.txt contains it is run Output : Line Number : 2 Error Position : 1 Total Errors
2 min read
C Program To Merge Two Arrays
To merge 2 arrays in C language we will use the following approaches: Using Quaint Methodology Using Functions Input: arr1 = [1, 2, 3, 4, 5] arr2 = [6, 7, 8, 9, 10] Output: arr3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]1. Using Quaint Methodology C/C++ Code // C Program To Merge Two Arrays // using Quaint Methodology #include &lt;stdio.h&gt; int main() { i
2 min read
Article Tags :