C program to copy the content of one file into another using fputc

C program to copy the content of one file into another using fputc:
If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().

#include<stdio.h>
#include<stdlib.h>

main()
{
   FILE *fp1,*fp2;
   char a;

   fp1 = fopen("reading.txt","r");
   if(fp1 == NULL)
    {
      puts("Cannot open this file");
      exit(1);
    }

   fp2 = fopen("writting.txt","w");
   if(fp2 == NULL)
    {
      puts("Unable to open this file");
      fclose(fp1);
      exit(1);
    }

   do
    {
      a = fgetc(fp1);
      fputc(a,fp2);
    }while (a != EOF);

   fclose(fp1);
   fclose(fp2);
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.