Many applications run with command line parameters. In fact, these command line parameters can also be implemented in C language programs, the flexible use of command line parameters can effectively improve the program running efficiency and get twice the result with half the effort.
The command line parameters in the C language involve the main (int argc, char * argv []) parameters of the program, int argc indicates the number of command line parameters (including the executable program name itself). char * argv [] indicates the specific content of each parameter, argv [0] indicates the executable program name in the command line, argv [1] indicates the content of the second parameter in the command line, and so on. The following example shows the number and content of the command line parameters:
Main (int argc, char * argv [],
{Int I;
Printf (Executable File name in the command line: % s, argv [0]);
Printf (a total of % d parameters:, argc );
I = 0;
While (argc> = 1)
{Printf ("% s, argv [I ++]);
Argc --;}
}
The command line parameters are most used in command lines with drive letters, paths, or file names, such as dir, therefore, flexible processing of such parameters can effectively improve the running effect of the program. For example, the DIR command can be followed by a drive letter, a path, or a file name. How can this parameter be distinguished? See the following example (this program simulates the DIR command. The program requires that you enter a parameter on the command line: drive letter, path, or file name, if there is no parameter or more than one parameter, the default parameter "*. *").
*--------------------
Function: simulates the DIR command to process command line parameters.
--------------------*/
# 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 the command line parameter 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 drive letter, add ″*.*″*/
Getcwd (path1, 50);/* obtain the current path */
If (chdir (ss) = 0)/* determines whether the parameter is a path */
Strcat (ss, \ *. *);/* if the path is not included at the end, add *.**/
Chdir (path1);/* restore the original path */
Strcpy (path2, ss );
For (j = strlen (path2); j> 0; j --)/* extract the path from 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 contain a path, take the current path */
Strcpy (path2, path1 );
Printf (** simulate DIR ** 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 (% 15 s % 20ld, f. ff_name, f. ff_fsize );
Else
Printf (& 11 s, f. ff_name );
Num ++;
J ++;
If (j = 23)
Printf (-------- More (press any key to continue )----);
Getch ();
J = 0;
Printf ("(directory path % s)", path2 );}
Done = findnext (& f);/* Find the next matching file */
}
Printf ("the current directory contains a total of % d Files.", num );