Write a C program to Merge two files using file handling

By | 01.12.2016

Merge two files using file handling


Write a C program to Merge two files using file handling. Here’s simple program to Merge two files using file handling in C Programming Language.


This c program merges two files and store their contents in an another file. The files which are to be merged are opened in read mode and the file which contains content of both the files is opened in write mode.

To merge two files first we open a file and read it character by character and store the read contents in another file then we read the contents of another file and store it in file, we read two files until EOF ( end of file ) is reached.

Below is the source code for C program to Merge two files using file handling which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C program to Merge two files using file handling  */

/* -----------------file4.txt contains text-------------

  Perpetual sincerity out suspected necessary
  one but provision satisfied. Respect nothing
  use set waiting pursuit nay you looking.
  If on prevailed concluded ye abilities.
  Address say you new but minuter greater.

-----------------file5.txt contains text-------------

  Do denied agreed in innate. Can and
  middletons thoroughly themselves him.
  Tolerably sportsmen belonging in september
  no am immediate newspaper. Theirs expect
  dinner it pretty indeed having no of.
  Principle september she conveying did
  eat may extensive.

-------------------------------------------------------*/


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int main()
{

        FILE *fs1, *fs2, *ft;
        char ch, file1[50], file2[50], file3[50];
        printf("Enter name of first file :: ");
        gets(file1);
        printf("\nEnter name of second file :: ");
        gets(file2);
        printf("\nEnter name of file which will store contents of two files :: ");
        gets(file3);
        fs1 = fopen(file1,"r");
        fs2 = fopen(file2,"r");
        if( fs1 == NULL || fs2 == NULL ) {
                perror("Error ");
                printf("Press any key to exit...\n");
                exit(EXIT_FAILURE);
        }
        ft = fopen(file3,"w");
        if( ft == NULL ) {
                perror("Error ");
                printf("Press any key to exit...\n");
                exit(EXIT_FAILURE);
        }
        while( ( ch = fgetc(fs1) ) != EOF )
              fputc(ch,ft);
        while( ( ch = fgetc(fs2) ) != EOF )
              fputc(ch,ft);
        printf("\nTwo files were merged into %s file successfully.\n",file3);
        fclose(fs1);
        fclose(fs2);
        fclose(ft);

        return 0;
}

OUTPUT : :


/*  C program to Merge two files using file handling  */

-----------------------------------------------------------------------------

Enter name of first file :: C:\\Users\\acer\\Documents\\file4.txt

Enter name of second file :: C:\\Users\\acer\\Documents\\file5.txt

Enter name of file which will store contents of two files :: C:\\Users\\acer\\Documents\\file6.txt

Two files were merged into C:\\Users\\acer\\Documents\\file6.txt file successfully.

Above is the source code for C program to Merge two files using file handling which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

0 0 votes
Article Rating
Category: C Programming File Handling Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments