Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

command line arguments to copy paste in c

//I found this helpful

#include <stdio.h>

int main(int argc, char *argv[]) 
{
  // file pointers
  FILE
    *fptr1,
    *fptr2;

  // variables
  int i;
  char ch;

  printf("Total number of argument passed: %d
", argc);

  // open source file in read mode
  // if error
  // then, exit
  if( (fptr1 = fopen(argv[1], "r") ) == NULL ) {

    printf("Error...
Cannot open file: %s
", argv[1]);
    printf("Either the filename is incorrect or it does not exists.
");

    return -1;

  }

  // check if the destination file exists
  if( (fptr2 = fopen(argv[2], "r") ) != NULL) {

    printf("Warning: File %s already exists.
", argv[2]);
    printf("Press Y to overwrite. Or any other key to exit: ");
    
    // take user input
    ch = getchar();

    // if user don't want to overwrite
    // then, exit
    if(ch != 'Y' && ch != 'y') {
        
      printf("Terminating the copy process.
");

      // close connection
      fclose(fptr1);
      fclose(fptr2);
      
      return -1;

    }
    else {
      // close read mode connection
      fclose(fptr2);
        
      // now open destination file in write mode
      fptr2 = fopen(argv[2], "w");
    }

  }
  // if destination file doesn't exists
  // then, open destination file in write mode
  else {
    fptr2 = fopen(argv[2], "w");
  }

  // copy the content of source to destination
  while( !feof(fptr1) ) {
    ch = getc(fptr1);
    if(ch != EOF) {
      putc(ch, fptr2);
    }
  }

  // close connection
  fclose(fptr1);
  fclose(fptr2);

  printf("Content of %s copied to %s
", argv[1], argv[2]);

  return 0;
}
Source by dyclassroom.com #
 
PREVIOUS NEXT
Tagged: #command #line #arguments #copy #paste
ADD COMMENT
Topic
Name
6+5 =