C Programming
In this tutorial we will learn to use command line arguments in C programming language.
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
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 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.
Element | Value |
---|---|
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.
/**
* 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.
ADVERTISEMENT