Search
 
SCRIPT & CODE EXAMPLE
 

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;
}
Comment

PREVIOUS NEXT
Code Example
C :: asasz 
C :: YOUNG SEX PARTY underground collection 
C :: Handling exceptions during datetime conversion 
C :: man write c 
C :: djb2 algorithm for C 
C :: c limit value range 
C :: gtk widget change window title 
C :: converting time in c 
C :: Trier lexicographiquement en c 
C :: Array in element from lowest 
C :: add last in list c 
C :: command line coursera 
C :: C #define preprocessor 
C :: C Create union variables 
C :: Manage Menu Driven Program using switch statement 
C :: attiny pinout 
C :: print binary in c 
C :: Word Processor, Spreadsheet and Presentation Software are the examples of 
C :: perfect numbers in c 
C :: c make list 
C :: 4k stogram chave 
Dart :: flutter column center horizontal text 
Dart :: text fieldform color flutter 
Dart :: card border radius in flutter 
Dart :: how to print in the same line in dart 
Dart :: flutter portrait only 
Dart :: flutter path join 
Dart :: flutter listtile color 
Dart :: random number dart with length 7 
Dart :: flutter check if platform is ios or andriod 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =