"With the explanations in the preceding sections, you should now be able to perform basic file processing."
However, it's tedious to enter the filename every time.
It would be incredibly convenient if we could open files by simply dragging them from programs like File Explorer.
Actually, in the Windows command prompt window,
Drag a file, and the filename will be automatically entered.
With this method, entering file names will be easy.
However, you can't do that if you drag a file onto an executable.
C has the capability to receive a filename as an argument when an application is launched.
That is the
command-line argument.
【Command-line arguments】
A string passed at application startup.
Specifies the filenames and operational options to be primarily processed.
"We have previously declared the arguments of the main function as void,"
Actually, you can specify arguments of a defined type here.
"To receive command-line arguments, specify the following arguments:"
int main(int argc, char* argv[]);
argc is the number of command-line arguments, and argv is a pointer variable to an array of characters.
"Character array pointer variables might seem difficult to use, don't they?"
Actually, it's just like this.
This way, you can display the zero-th (first) command line.
Changing the number of elements in argv[0] allows you to access the corresponding command line.
The first command-line argument is the application's filename.
The filename dragged from Explorer (or similar file explorers) is stored in the first position.
The following program is an example of displaying the name of a dragged file.
#include <stdio.h>
int main(int argc, char* argv[])
{
if (argc > 1) {
printf("%s\n", argv[1]);
}
fflush(stdin);
getchar();
return 0;
}
Check the number of command-line arguments with `argc`; if it's greater than one, assume there are command-line arguments.
Displaying the contents of the first command line.
fflush(stdin);
getchar();
The above two lines are for stopping the screen display.It will wait for execution and terminate with an appropriate key.
なお、筆者開発の
Learning C Development Environmentをお使いの場合は、
The above two lines are not needed.It automatically stops.
上の命令は、出力バッファを強制出力するfunctionである fflush で、
Clearing the input buffer, stdin.
This is not the correct way to use it.邪道is.
Please use this method only for practice and do not use it in actual app development.
The results of dragging a random file in Explorer are as follows.
The dragged file's name is displayed with its full path.
ほとんどの場合、コンパイルしたSource fileと同じフォルダの中に、
The executable is being created with the same name as the source file.
Generally, the command line includes items other than filenames.
You may need to specify options that govern the behavior of an application.
For example, if you start Windows by typing "defrag" in the Run dialog box,
The defrag process starts but then exits without doing anything.
Typing "defrag c:" will start defragmenting the C drive.
"Also, specifying `defrag c: -a` will display only the analysis results for the C drive."
This is a common practice for advanced computer users.
In this example, two strings, "c:" and "-a", are passed to the command line.
It's being analyzed within the application to determine its behavior.
The same can be easily seen by examining the command-line string.
Here's an example analyzing the presence or absence of the options -a and -s.
#include <stdio.h>
int main(int argc, char* argv[])
{
while (argc > 0) {
argc--;
if (argv[argc][0] == '-') {
if (argv[argc][1] == 'a')
printf("-a オプション\n");
if (argv[argc][1] == 's')
printf("-s オプション\n");
}
}
return 0;
}
The results of running this program with the options -a -s are as follows.
If you do it that way, you can handle any number of options.
"Also, if we can handle strings that don't start with '-' as filenames,"
The filename can also be obtained in the same way as in the previous section.