In many applications run with command line parameters, in fact, these command-line parameters in the C language program can also be implemented, flexible use of command-line parameters to deal with can effectively improve the operation of the program efficiency, received a multiplier effect.
The command line arguments in C language involve two parameters such as main function main (int Argc,char *argv[], where int argc represents the number of command-line arguments (including the executable name itself), and Char *argv[] represents the specific contents of each parameter. Argv[0] is the contents of the second parameter in the command line, Argv[1, and so on, for the executable program name itself. The following example outputs the number of command line arguments and the contents of the parameters:
Main (int Argc,char *argv[],
{int I;
printf (the executable file in the N command line is named:%s,argv[0]);
printf (n altogether has%d parameters:, ARGC);
i=0;
while (argc>=1)
{printf (″%s, argv[i++]);
argc--;}
}
Command-Line arguments are used at most or in a command line with a disk, path, or file name, such as Dir A:, so the flexibility to handle this type of parameter can effectively improve the program's performance. For example, the dir command, which can then be a letter, can be a path, or can be a filename, how to distinguish this parameter? Take a look at the following example (this program simulates the dir command, which requires you to enter a parameter on the command line: the letter or path or file name, and the default parameter "*.*" if there are no arguments or more than one parameter).
*--------------------
Function: Simulate the dir command to process command line arguments
--------------------*/
#include
#include
#include
#inchlude
int j,num=0;
Char ss[20],path[50],path2[50];
void Main (int argc,char *argv[])
{
struct FFBLK f;
int done;
if (argc==2)/* Fetch command line arguments to the array * *
strcpy (ss,argv[1]);
Else
strcpy (Ss,″*.*″); /* Assign the default parameter to the array/*
if ((Ss[strlen (ss) -1]==′′| | ((Ss[strlen (ss) -1]== ': '))
strcat (Ss,″*.*″); /* If the parameter is a path or a letter, add ″*.*″*/
GETCWD (path1,50); /* Take the current path * *
if (ChDir (ss) ==0)/* Determine whether the parameter is a path * *
strcat (ss,*.*); * * If there is no belt at the end of the path, add *.* * *
ChDir (path1); /* Restore the original path * *
strcpy (PATH2,SS);
For (J=strlen (path2); j>0;j--)/* Extract the path in the parameter to path2 * *
{if ((path2[j]== ')) | | (path2[j]== ': ')) {
Path2[j+1]= ';
Goto Senull;}
}
Path2[0]= ';
Senull:
if (strlen (path2) ==0)/* If the given parameter does not have a path, take the current path * *
strcpy (PATH2,PATH1);
printf (n** analog dir**n Command directory path%s,path2);
Done=findfirst (ss,&f,55); /* Find the first matching file * *
J=1;
while (!done)
{if (f.ff_attrib!=0x10)/* If the file attribute is not a directory * *
printf (n%15s%20ld,f.ff_name,f.ff_fsize);
Else
printf (n &11s, f.ff_name);
num++;
j + +;
if (j==23)
printf (n--------More (press any key to continue)----);
Getch ();
j=0;
printf (″n (directory path%s) ″,path2);
Done=findnext (&F); /* Find the next matching file * *
}
printf (a total of%d files in the current directory of ″n. N″,num);