Name this program duplicatec The program takes two command

Name this program duplicate.c – The program takes two command line arguments, the name of an input file and an output file. Your program should confirm the input file exists (exiting with an appropriate error message if it does not). If the file exists, copy the input file to the output file specified on the command line. Sample executions are shown below (with the second one giving an invalid input file)

File copied

Solution

Answer:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

int main(int argc, char* argv[]) {
   FILE *inputFile, *outputFile;
   char ch;
  
   //Opening input file in read mode
   inputFile = fopen(argv[1], \"r\");
   //Opening output file in write mode
   outputFile = fopen(argv[2], \"w\");
   //checking if input file exists
   if(inputFile == NULL)
   {
       printf(\"The file specified, %s, does not exist.\", argv[1]);  
       return -1;
   }
  
   while(1)
   {
       //getting each character from inputFile
       ch = fgetc(inputFile);
       if(ch == EOF)
           break;
       else
           putc(ch, outputFile);
   }
  
   printf(\"File copied\");
  
   //closing both the file pointers
   fclose(inputFile);
   fclose(outputFile);
   return 0;
}

Name this program duplicate.c – The program takes two command line arguments, the name of an input file and an output file. Your program should confirm the inpu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site