MMGameslogo  MMGames
TwitterSharebutton  FacebookSharebutton   
learn through sufferingC Language
learn through sufferingC Language

Drug response

Retrieving the dragged file name
"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:"

A main function that accepts command-line 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.

Displaying command-line arguments
printf("%s",argv[0]);

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.

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-line arguments with `argc`; if it's greater than one, assume there are command-line arguments.
Displaying the contents of the first command line.

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

fflush(stdin) is a hack.
上の命令は、出力バッファを強制出力する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.

Execution results
D:\BCPad\Source\test.exe

The dragged file's name is displayed with its full path.

Executable location
ほとんどの場合、コンパイルしたSource fileと同じフォルダの中に、
The executable is being created with the same name as the source file.

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

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 オプション\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.

Execution results
-a オプション
-s オプション

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.


About This Site

Learning C language through suffering (Kushi C) is
This is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.

Part 0: Program Overview
  1. What is a program?
Chapter 3: Displaying on the Screen
  1. String Display
  2. newline character
  3. Practice Problem 3
Chapter 4: Displaying and Calculating Numbers
  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4
Chapter 6: Input from the Keyboard
  1. input function
  2. The fear of input
  3. Practice Problem 6
Chapter 9: Repeating a Fixed Number of Times
  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9
Chapter 10: Repeating Without Knowing the Number of Times
  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10
Chapter 13: Handling Multiple Variables at Once
  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13
Chapter 19: Dynamic Arrays
  1. Create arrays freely.
  2. Practice Problem 19