C language learned by suffering
C language learned by suffering
Drug compliance
Retrieving the name of a dragged file
The previous sections have enabled you to perform a minimum of file processing.
However, it is tedious to enter the file name each time.
It would be very convenient to be able to open the file by dragging it from Explorer, etc.
In fact, in the Windows command prompt screen, the
If you drag a file, the file name is automatically entered.
This method makes it easy to enter file names.
However, this is not possible if the file is dragged to an executable file.
The C language has the ability to pass a file name when starting an application.
That is the command line argument.
So far, the arguments of the main function have been declared as void types.
In fact, you can specify a defined type of argument here.
To accept command line arguments, specify the following arguments
argc is the number of command lines, and argv is a pointer variable to a character array.
A pointer variable to a character array may sound difficult to use.
In practice, it is simply as follows
In this way, the 0th (first) command line can be displayed.
By changing the number of elements in argv[0], you can access the corresponding command line.
The zeroth command line is the filename of the application itself.
File names dragged from Explorer, etc. are stored in the first position.
The following program is an example of displaying the name of a dragged file.
Check the number of command lines with argc, and if there are more than one, determine that there is a command line, and then run
The contents of the first command line are displayed.
The top two lines are for stopping the display of the screen. It waits for execution and ends with the appropriate key.
If you are using the author's C language development environment for learning, you can use the
The above two lines are not necessary. It will stop automatically.
The following is the result of dragging the appropriate file in Explorer.
The name of the dragged file is shown with its full path.
However, it is tedious to enter the file name each time.
It would be very convenient to be able to open the file by dragging it from Explorer, etc.
In fact, in the Windows command prompt screen, the
If you drag a file, the file name is automatically entered.
This method makes it easy to enter file names.
However, this is not possible if the file is dragged to an executable file.
The C language has the ability to pass a file name when starting an application.
That is the command line argument.
Keywords.
[Command line arguments
A string passed at application startup.
It mainly specifies the name of the file to be processed and operation options.
So far, the arguments of the main function have been declared as void types.
In fact, you can specify a defined type of argument here.
To accept command line arguments, specify the following arguments
main function that accepts command line arguments
int main(int argc, char* argv[]);
argc is the number of command lines, and argv is a pointer variable to a character array.
A pointer variable to a character array may sound difficult to use.
In practice, it is simply as follows
Display command line arguments
printf("%s",argv[0]);
In this way, the 0th (first) command line can be displayed.
By changing the number of elements in argv[0], you can access the corresponding command line.
The zeroth command line is the filename of the application itself.
File names dragged from Explorer, etc. are stored in the first position.
The following program is an example of displaying the name of a dragged file.
Source Code
#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 lines with argc, and if there are more than one, determine that there is a command line, and then run
The contents of the first command line are displayed.
source code
fflush(stdin);
getchar();
The top two lines are for stopping the display of the screen. It waits for execution and ends with the appropriate key.
If you are using the author's C language development environment for learning, you can use the
The above two lines are not necessary. It will stop automatically.
fflush(stdin) is evil
The above instruction is fflush, a function that forces the output buffer to
The input buffer, stdin, is being cleared.
This is not correct usage. It is an evil way.
This usage should only be used in the practice stage and should not be used in actual application development.
The input buffer, stdin, is being cleared.
This is not correct usage. It is an evil way.
This usage should only be used in the practice stage and should not be used in actual application development.
The following is the result of dragging the appropriate file in Explorer.
Execution Result
D:\BCPad\Source\test.exe
The name of the dragged file is shown with its full path.
Executable File Location
In most cases, you will find the compiled source files in the same folder as the
An executable file is created with the same name as the source file name.
An executable file is created with the same name as the source file name.
Option Analysis
Generally, the command line contains a filename as well as a
Options that specify the behavior of the application may be specified.
For example, if you invoke Run on Windows with the filename defrag
Defrag will start and exit without doing anything, but
defrag c: will start defragmenting the C drive.
Also, if defrag c: -a is specified, only the analysis results for the C drive are displayed.
This usage is familiar to advanced computer users.
It is then parsed within the application to determine its behavior.
Something similar can easily be found by examining the command line string.
Here is an example of parsing the presence or absence of the options -a and -s.
The following is the result of running this program with the option -a -s.
Do the same for any number of options.
You can also treat a string that does not start with a - as a filename by using the
File names can be obtained in the same way as in the previous section.
Options that specify the behavior of the application may be specified.
For example, if you invoke Run on Windows with the filename defrag
Defrag will start and exit without doing anything, but
defrag c: will start defragmenting the C drive.
Also, if defrag c: -a is specified, only the analysis results for the C drive are displayed.
This usage is familiar to advanced computer users.
In this example, two strings, c: and -a, are passed to the command line, and
It is then parsed within the application to determine its behavior.
Something similar can easily be found by examining the command line string.
Here is an example of parsing the presence or absence of the options -a and -s.
Source Code
#include <stdio.h>
int main(int argc, char* argv[])
{
while (argc > 0) {
argc--;
if (argv[argc][0] == '-') {
if (argv[argc][1] == 'a')
printf("-a options\n");
if (argv[argc][1] == 's')
printf("-s options\n");
}
}
return 0;
}
The following is the result of running this program with the option -a -s.
Execution Result
-a option
-s option
-s option
Do the same for any number of options.
You can also treat a string that does not start with a - as a filename by using the
File names can be obtained in the same way as in the previous section.
About this Site
The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or more complete than any book on the market.