MMGames Introduction to C C Language Development Environment C language now Useful Apps Contact Us
MMGames

Automatic version identification

SpineViewer

It's easy to tell by looking at it.

Response Time Checker

I can leave my computer on and do it.

Mouse cleaning time

I can leave my computer on and do it.

Mouse cleaning time

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.

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 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.

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.

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

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.

Part 0: Program Overview
  1. What is the program?
Chapter 2: How to write a program
  1. Writing Rules
  2. Writing conventions
  3. Exercise 2
Chapter 3: Display on Screen
  1. String display
  2. newline character
  3. Exercise 3
Chapter 4: Numeric Display and Calculation
  1. Numeric Display
  2. Basic Calculations
  3. Type of value
  4. Exercise 4
Chapter 5: Numerical Memory and Calculation
  1. Memorize values
  2. Variable Type
  3. Type conversion
  4. Numeric justification
  5. Exercise 5
Chapter 6: Input from the keyboard
  1. Functions for input
  2. Fear of Input
  3. Exercise 6
Chapter 9: Repetition with a fixed number of times
  1. Sentences that repeat themselves
  2. Loop Operation Mechanism
  3. Exercise 9
Chapter 10: Unknown number of repetitions
  1. Loop of unknown frequency
  2. input check
  3. Exercise 10
Chapter 13: Handling Multiple Variables at Once
  1. Multiple variables are handled together.
  2. How to use arrays
  3. Exercise 13
Chapter 19: Dynamic Arrays
  1. Create arrays at will
  2. Exercise 19
Chapter 20: Multiple Source Files
  1. Minimal division
  2. The Stone of Partition
  3. Exercise 20

Comment
COMMENT

Open the 💬 comment submission box.