Exercise 6. WAP to merge contents of two files into another file. 1
#include<stdio.h>
int main()
{
FILE *fp1, *fp2, *fp3;
int line, n=1;
char text[80];
fp1=fopen("file1.txt","r");
fp2=fopen("file2.txt","r");
fp3=fopen("merge.txt","w");
if (!fp1 || !fp2) //check if file exists
{
printf("File not found!");
return 0; //terminates the program
}
do{
fgets(text,80,fp1); //read line from file
fputs(text,fp3); //write line in file
}while(!feof(fp1));
fputs("\n",fp3);
do{
fgets(text,80,fp2); //read line from file
fputs(text,fp3); //write line in file
}while(!feof(fp2));
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}
first line
second line
third line
third line
forth line
first line
second line
third line
third line
forth line