Showing posts with label Copy One File to Another. Show all posts
Showing posts with label Copy One File to Another. Show all posts

Thursday, December 1, 2011

Copy One File to Another


/* Write a C program which copies one file to another.*/
#include <stdio.h>
#include <conio.h>
#include <process.h>
void main(int argc, char *argv[])
{
 FILE *fs,*ft;
 char ch;
 clrscr();
 if(argc!=3)
 {
  puts("Invalid number of arguments.");
  exit(0);
  }
 fs = fopen(argv[1],"r");
 if(fs==NULL)
 {
 puts("Source file cannot be opened.");
 exit(0);
 }
 ft = fopen(argv[2],"w");
 if (ft==NULL)
 {
 puts("Target file cannot be opened.");
 fclose(fs);
 exit(0);
 }
 while(1)
 {
  ch=fgetc(fs);
  if (ch==EOF)
  break;
  else
  fputc(ch,ft);
  }
  fclose(fs);
  fclose(ft);
  getch();
}