Thursday, 27 July 2017

Write a program to copy a file into another file.

Q : Write a program to copy a file into another file.

Answer : 


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main(int argc,char *argv[])
{
     FILE *infile,*outfi
le;
     char ch;
     if(argc>3)
     {
          printf("Only Source and file name is allowed");
          exit(1);
     }
     else
     {
          infile=fopen(argv[1],"r");
          if(infile==NULL)
          {
              printf("Source file opening error");
              exit(2);
          }
          else
          {
              outfile=fopen(argv[2],"w");
              if(outfile==NULL)
              {
                   printf("Unable to create destination file");
                   exit(3);
              }
              else
              {
                   while(1)
                   {
                        ch=fgetc(infile);
                        if(ch==EOF)
                             break;
                        fputc(ch,outfile);

                   }
                   printf("\nFile Copied Successfully");
              }
          }
          fclose(infile);
          fclose(outfile);

     }

}