Tuesday, 14 July 2020

File writing program append mode using Command Line Argument

Q : Write a program using command line parameters to append one text file to another text 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],"a");
              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);

     }

}



No comments:

Post a Comment