Monday, 31 August 2015

C Program to merge 2 files

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
 FILE *fs1,*fs2,*ft;
 char ch,file1[20],file2[20],file3[20];
 clrscr();
 printf("Enter name of first file\n");
 gets(file1);
 printf("Enter name of second file\n");
 gets(file2);
 printf("Enter name of file which'll store contents:\n");
 gets(file3);
 fs1=fopen(file1,"r");
 fs2 = fopen(file2,"r");
 if(fs1==NULL || fs2==NULL )
 {
  printf("Error opening file\n");
  getch();
  exit(1);
 }
 ft=fopen(file3,"w");
 if(ft==NULL )
 {
  printf("Error opening file\n");
  exit(1);
 }
 while((ch=fgetc(fs1))!=EOF)
  fputc(ch,ft);
 while((ch=fgetc(fs2))!=EOF)
  fputc(ch,ft);
 printf("Two files were merged into %s file \n",file3);
 fclose(fs1);
 fclose(fs2);
 fclose(ft);
 getch();
}

Output

Leave a Reply

 
 

Labels

Blog Archive