Command Line Argument


The process of passing arguments to the main function while executing the program is known as command line argument.
The general form of main function that accepts command line arguments is given below:
main (int argc, char * argv [])
Here,
argc
means argument counter. It contains number of arguments passed to the main function while executing the program.

argv means argument vector. It is an array of character pointers that contains actual arguments that are passed to the main function while executing the program.
The first argument is contained in argv [0], second argument is contained in argv [1] and so on.
The First argument is always the path name of the program that you want to execute. All the arguments must be separated by space.

     

#include<stdio.h>
#include<conio.h>
void main(int argc, char *argv[])
{
FILE *fp1,*fp2;
char ch;
clrscr();
if (argc!=3)
      printf("Wroong Number of Arguments\n");
else
{
fp1=fopen(argv[1],"r");
fp2=fopen(argv[2],"w");
while((ch=getc(fp1))!=EOF)
{
      putc(ch,fp2);
}
printf("File Copied\n");
fclose(fp1);
fclose(fp2);
}
getch();

Go to the Dos Shell and type following at command prompt to execute main () function:
C:\Tc\Source> COM Test1 Test2
It will copy contents of file test1 into Test2.

Download Projects


Download Programs