It came to be able to do a minimum file processing by the explanation to the preceding clause.
However, it is troublesome to input the file name every time.
It is very convenient if it drags from the Explorer etc. and it comes to open it.
Actually, on the command prompt screen of Windows
If the file is dragged, the file name is automatically input.
If it is this method, the file name can be easily input.
However, when the file is dragged in the execution file, it is not possible to do.
In C language, the function to pass the file name when the application is started is provided.
It is a command line argument.
Though the argument of the main function has been declared up to now as void type
[ Command line argument ]
Character string passed when application starts
Specify the file name and the optional operation chiefly processed.
Argc is a number of command lines, and argv is pointer variable to the character array.int main(int argc,char *argv[]);
The command line 0(..the first.. beginning) can be displayed if it does like this.printf("%s",argv[0]);
The 0th command line becomes the file name of the application.
The file name dragged from the Explorer etc. is stored in the first.
The following program is an example of displaying the dragged file name.
It is judged that the number of command lines is confirmed with argc, and there is a command line if it is more than one.#include <stdio.h> int main(int argc,char *argv[]) { if (argc > 1) { printf("%s\ n",argv[1]); } fflush(stdin); getchar(); return 0; }
Two lines above are the processing to stop the display of the screen. Become an execution waiting, and end with a suitable key.fflush(stdin); getchar();
The result of dragging a suitable file by the Explorer becomes as follows.
[ Fflush(stdin) is evil ways ].
The instruction above is a function that compelling outputs the output buffer With fflush
It is an input buffer Stdin is cleared.
This usage is evil ways that can be used only by a part of compiler.
Do not use this use only at the practice stage, and do not use it in real development.
The name of the dragged file is displayed in the full path.
D:\BCPad\Source\test.exe
[ Position of execution file ]
To the same folder as the compiled source file in many cases
The execution file is made by the same name as the source file name.
Generally, besides the file name ..the command line...
The option that specifies the operation of the application might be specified.
For instance, in execution specifying the file name with Windows When starting specifying defrag
Though it ends without defrag's starting and doing anything
Begin the defrag of C drive when specifying defrag c.
Moreover, display only the analysis result of C drive when specifying defrag c:-a.
Such a usage is a familiar way for the personal computer senior.
In this example, two character strings named c and ..-.. a are passed to the command line.
It is analyzed in the application, and operation has been decided.
If the character string of the command line is examined, similar is easily understood.
..-.. ..a.. - here It is an analysis example as for the presence of the option of s.
..this program.. - The option named a -s is given and the result of execution is as follows.#include <stdio.h> int main(int argc,char *argv[]) { while (argc > 0) { argc--; If (argv[argc][1] =='a') printf ("Option \n of - a"); if (argv[argc][1] =='s') Printf ("Option \n of - s"); } } return 0; }
It is possible to correspond to the option how many if keeping similar.
- A is optional.
- S is optional.