Thread: reading in command line arguments

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    reading in command line arguments

    i have been watching a quick video on using argc and *argv[]. he used debug as an example for the code but didn't go any further with it other than printing out a line saying "in debug mode".

    how does one actually implement it do you redefine a #define debug or have some other global variable and continually test what mode your in?

    coop

  2. #2
    Registered User catacombs's Avatar
    Join Date
    May 2019
    Location
    /home/
    Posts
    81
    To read in command arguments, the video is correct, you would use argc, the number of arguments passed to the program, and *argv[] or **argv, an array of character string arguments passed.

    Here's an example:

    Code:
    #include <stdio.h>
    
    int main(int argc, char** argv)
    {
        // get the first argument
        // the name of the program
        char* first_arg = argv[0];
        printf("Name of file: %s\n", first_arg);
    
        // get the second argument
        // what you're probably looking for
        char* second_arg = argv[1];
        printf("Input: %s\n", second_arg);
    
        for (int i = 0; i < argc; i++) {
            printf("Argument [%d] %s\n", i, argv[i]);
        }
    
        return 0;
    }
    You can send the program as many arguments as you want. argc will increasse the argument count and **argv will put them in an array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading arguments from command-line
    By SafetyMark in forum C Programming
    Replies: 2
    Last Post: 10-27-2016, 04:30 PM
  2. reading in command line arguments from a file?
    By g1i7ch in forum C Programming
    Replies: 30
    Last Post: 06-22-2006, 01:35 PM
  3. Command Line Arguments...again
    By axon in forum C++ Programming
    Replies: 2
    Last Post: 09-24-2003, 03:35 AM
  4. reading command line arguments into buffer
    By jpp1cd in forum C Programming
    Replies: 2
    Last Post: 12-12-2002, 08:08 PM
  5. Command line arguments
    By Yelagin in forum C Programming
    Replies: 1
    Last Post: 07-30-2002, 02:41 PM

Tags for this Thread