C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Command Line Arguments

C Programming

In this tutorial we will learn to use command line arguments in C programming language.

What is command line arguments?

Command line arguments are the parameters supplied to a program when they are invoked.

For example, lets say we have a program copyfile which takes two arguments which are file names where, the first file name is the source file and the second file name is destination file. And this program copies the content of the first file in the second file.

Windows:

E:\TC\BIN>copyfile source_filename destination_filename

Mac/Linux:

$ ./copyfile source_filename destination_filename

The main function

Every C program has the main() function which marks the starting point of the code execution.

So, far we have not passed any argument to the main() function hence we were setting void.

int main(void) {
  // some code goes here...

  return 0;
}

The argc and argv arguments

The main() function can take two arguments namely argc and argv.

int main(int argc, char *argv[]) {
  // some code goes here...

  return 0;
}

The argc is the argument counter that counts the number of arguments passed on the command line.

The argv is the argument vector representing an array of character pointers pointing at the command line arguments. And the size of this array of pointers is equal to the value of argc.

In the following example we are executing the copyfile program and passing two file names (source and destination).

Windows:

E:\TC\BIN>copyfile original.txt duplicate.txt

Mac/Linux:

$ ./copyfile original.txt duplicate.txt

So, argc for the above example is 3. And the array of pointers argv is as follows.

ElementValue
argv[0]copyfile
argv[1]original.txt
argv[2]duplicate.txt

Note! The first parameter of the command line argument always represents the program name.

Write a program in C to copy the content from one file to the next

/**
 * file: copyfile.c
 * author: yusuf shakeel
 * date: 2011-04-20
 * description:
 * COMMAND LINE ARGUMENT
 * user defined command: copyfile
 * This command allows the user to copy the content of file1 to file2.
 * if file2 already exists then overwrite only after taking user permission.
 * 
 * syntax:
 * $ ./copyfile <file1.extension_name> <file2.extension>
 * 
 * where,
 * file1 is the source file
 * file2 is the target file where content of file1 is copied.
 * 
 * example:
 * $ ./copyfile original.txt duplicate.txt
 */

#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\n", argc);

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

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

    return -1;

  }

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

    printf("Warning: File %s already exists.\n", 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.\n");

      // 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\n", argv[1], argv[2]);

  return 0;
}

Output: Running for the first time.

YUSUF-MBP:c-project yusufshakeel$ gcc copyfile.c -o copyfile
YUSUF-MBP:c-project yusufshakeel$ ./copyfile original.txt duplicate.txt
Total number of argument passed: 3
Content of original.txt copied to duplicate.txt

Output: Running for the second time.

YUSUF-MBP:c-project yusufshakeel$ ./copyfile original.txt duplicate.txt
Total number of argument passed: 3
Warning: File duplicate.txt already exists.
Press Y to overwrite. Or any other key to exit: y
Content of original.txt copied to duplicate.txt

Output: Running again but this time terminating the process.

YUSUF-MBP:c-project yusufshakeel$ ./copyfile original.txt duplicate.txt
Total number of argument passed: 3
Warning: File duplicate.txt already exists.
Press Y to overwrite. Or any other key to exit: n
Terminating the copy process.

Content of the files.