Program to merge
the contents of two files
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
char c;
FILE *fptr1, *fptr2, *fptr3;
clrscr();
printf("Enter the text to be stored in the file - 1.\n");
printf("Use ^Z or F6 at the end of the text and press ENTER : \n\n");
fptr1 = fopen("FIRST.DAT","w");
while((c = getc(stdin)) != EOF)
fputc(c, fptr1);
fclose(fptr1);
printf("\nEnter the text to be stored in the file - 2.\n");
printf("Use ^Z or F6 at the end of the text and press
ENTER : \n\n");
fptr2 = fopen("SECOND.DAT","w");
while((c = getc(stdin)) != EOF)
fputc(c, fptr2);
fclose(fptr2);
fptr1 = fopen("FIRST.DAT","r");
fptr2 = fopen("SECOND.DAT","r");
fptr3 = fopen("MERGE.DAT","w");
while((c = fgetc(fptr1)) != EOF)
fputc(c, fptr3);
fclose(fptr1);
while((c = fgetc(fptr2)) != EOF)
fputc(c, fptr3);
fclose(fptr2);
fclose(fptr3);
printf("\nThe content of the merged file is : \n\n");
fptr3 = fopen("MERGE.DAT", "r");
while((c = fgetc(fptr1)) != EOF)
putchar(c);
fclose(fptr3);
getch();
}
Output:
Enter the text to be stored in the file - 1.
Use ^Z or F6 at the end of the text and press ENTER :
Computer Practice - I
I - Semester
^Z
Enter the text to be stored in the file - 2.
Use ^Z or F6 at the end of the text and press ENTER :
Computer Practice - II
II - Semester
^Z
The content of the merged file is :
Computer Practice - I
I - Semester
Computer Practice - II
II - Semester
-
UpdatedDec 31, 2019
-
Views5,087
You May Like
Program to maintain employee details using structures
Program to maintain student details using structures
Check whether the person is eligible to vote or not
Print the numbers that are divisible by a given no.
Program to generate magic square
To sort the given numbers in ascending & descending order