C Program to Merge Two Files Into Third File

In this tutorial you will learn about the C Program to Merge Two Files Into Third File and its application with practical example.

In this tutorial, we will learn to create c program to merge two files using C programming language. Merging of two file means the contents of two files gets merged into the third file.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • C Input Output
  • C Pointers
  • C File Handling
  • C Strings

Create required files to merge two files

Let’s first create three files (inside the current project directory) as following:

  • f1.txt – first source file
  • f2.txt – second source file
  • f3.txt – target file

All of the above files must be saved in the folder where you are saving program file. Put the following content in corresponding file:

f1.txt

f2.txt

We will keep the third file named f3.txt blank. As we will write the merged content of both the files namely f1.txt and f2.txt into it.

How to Merge Two Files Into Third File?

In this C program we will merge two files(f1.txt and f2.txt) and copy their contents in third file (f3.txt). The files to be merged will be opened in “read” mode and the target file where the contents of both the files will be merged is opened in “write” mode. Initially first file (f1.txt) will be opened and being read through character by character and store its contents in the target file (f3.txt). Then the second file will be opened in same way and being read through character by character and store its contents in the target file (f3.txt).The files are read until their EOF (end of file) is reached.

C Program to Merge Two Files Into Third File

In this program we will merge two files into third file. We would first declared and initialized the required variables. Next, we would prompt user to input source and target file names(f1.txt and f2.txt). Later in the program we will merge two files into third file(f3.txt) using c file handling functions. Now, if you open the file f3.txt, you will see that the content of both the files is copied and merged into it.

Output:-

c-program-to-merge-two-files

In the above program, we have first declared and initialized a set variables required in the program.

  • *fsOne = file pointer for first source file
  • *fsTwo = file pointer for second source file
  • *fTarget = file pointer for target file
  • fName1[20] = to hold first source file name
  • fName2[20] = to hold second source file name
  • fName3[30] = to hold target file name
  • ch = to read character

In the next statement user will be prompted to enter the two source file names which will be assigned to variable ‘fName1’ and ‘fName2’ respectively. Next, we will try to open source files using fopen() function. In case, if any of the file doesn’t exist or you don’t have the required  permission, then the function fopen() will returns NULL. Therefore, before starting the operation, we would have to check whether it returns NULL or not. If it returns NULL, then we will print an error message, otherwise will continue with the merge operation.

Now we will read through the character by character using fgetc() function, and will assign to FILE pointer fsOne and fsTwo one by one until it return EOF (End Of File). And using fputc() function, we will put the character into the target file fTarget. This way we get characters one by one from both the file and put into the third file. Now, if you open the file f3.txt, you will see that the content of both the files is copied and merged into it.

In this tutorial we have learn about the C Program to Merge Two Files Into Third File and its application with practical example. I hope you will like this tutorial.