Two files DATA1 and DATA2 contain sorted lists of integers. Write a C program to merge the contents of two files into a third file DATA i.e., the contents of the first file followed by those of the second are put in the third file.
Algorithm:
1. Open file1.txt and file 2.txt in reading 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.
Program Code:
#include<stdio.h>
void main()
{
FILE *fp1, *fp2,*fp3;
char ch;
fp1=fopen("program.txt","r");
fp2=fopen("new.txt","r");
fp3=fopen("merge.txt","a");
if(fp1==NULL||fp2==NULL||fp3==NULL)
{
printf("file opening problem");
return;
}
while(!feof(fp1))
{
ch=fgetc(fp1);
fputc(ch,fp3);
}
}
Thanks
Mukesh Rajput
Post A Comment:
0 comments: