Wednesday, March 23, 2016

C program to copy content of one file to other.

#include<stdio.h>
int main() {
   FILE *fp1, *fp2;
   char a;

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

   fp2 = fopen("test1.txt", "w");
   if (fp2 == NULL) {
      puts("test1.txt file error.");
      fclose(fp1);
      exit(1);
   }

   do {
      a = fgetc(fp1);
      fputc(a, fp2);
   } while (a != EOF);
  printf("test.txt is successfully copied to test1.txt.");
  fclose(fp1);
  fclose(fp2);
  return 0;
}

Output of the program

test.txt is successfully copied to test1.txt.


No comments:

Post a Comment