Tuesday, December 6, 2011

Write a C Program to copy a file to another file

#include <stdio.h>
#include <conio.h>
void main()
{
  FILE *p,*q;
  char file1[20],file2[20],ch;
  clrscr();
  printf("\nEnter the source file name to be copied: ");
  gets(file1);
  p=fopen(file1,"r");
  if(p==NULL)
  {
      printf("cannot open %s",file1);
  }
  printf("\nEnter the destination file name:");
  gets(file2);
  q=fopen(file2,"w");
  if(q==NULL)
  {
      printf("cannot open %s",file2);
  }
  while((ch=getc(p))!=EOF)
      putc(ch,q);
  printf("\nCOMPLETED");
  fclose(p);
  fclose(q);
 getch();
}

No comments:

Post a Comment