Q:

Write a C program to Merge two files using file handling

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 .

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a C Program to read student details and stor... >>
<< C program read integers and appends sum to the end...