learn through suffering C language learn through suffering 
C 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.

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

If you are using the author's C Language Development Environment for Learning, The above two lines are not needed.It automatically stops.

fflush(stdin) is a hack.
The command above uses the fflush function, which forces output buffering, to clear stdin, the input buffer. This is not the correct usage. It is a hack. Use this method only during practice sessions; do not use it in actual application development.

The results of dragging a random file in Explorer are as follows.

Results
D:\BCPad\Source\test.exe

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

Executable location
In most cases, an executable file with the same name as the source file is created in the same folder as the compiled 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 Options\n");
            if (argv[argc][1] == 's')
                printf("-s Options\n");
        }
    }

    return 0;
}

The results of running this program with the options -a -s are as follows.

Results
-a Options
-s Options

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

Loading comment system...