GIDNetwork > Command Line Arguments, Part 1
Register
« PHP function to ping Google Sitemaps Beginning Python Tutorial (Part 4) »

Command Line Arguments, Part 1

by: WaltP - Sep 02, 2005

Getting Data Without Operator Input

Command line arguments are a great way get information into your console mode programs without the need for input statements in your code. They are also useful when tunning program from a batch (script) file.

The first thing you need to know is how to access the parameters and how they are formatted.

Create the following program as arg.c and compile. I'm using C because I find the output easier, but it can be converted to C++ very easily.

C/CPP/C++ Code Example:

#include <stdio.h>
int main(int argc, char *argv[])
{
    int param;  // parameter index from the command line
    
    for (param= 0; param < argc; param++)
    {
        printf("%2d)  '%s' \n", param, argv[param]);
    }
    return argc;
}

Now run the program, adding command line parameters:

Generic Code Example:

d:\GIDNetwork\>arg testing 3 2 1 blast off
 0)  'd:\GIDNetwork\arg.exe'
 1)  'testing'
 2)  '3'
 3)  '2'
 4)  '1'
 5)  'blast'
 6)  'off'

d:\GIDNetwork\>

Notice the 0th arg is the file name. Your display may not have the path, but it will be the program name.

Each additional parameter is displayed as entered. They are all char* values. Now try adding quotes around a couple of parameters:

Generic Code Example:

d:\GIDNetwork\>arg testing "3 2 1" blast off
 0)  'd:\GIDNetwork\arg.exe'
 1)  'testing'
 2)  '3 2 1'
 3)  'blast'
 4)  'off'

d:\GIDNetwork\>

The quotes allow parameters to be combined as one. Also note that the numbers are in fact character strings and not integers. We'll get into how to use numbers as we continue.

Let's use the information above and write a file display program called DISP.exe, our own version of type or cat:

C/CPP/C++ Code Example:

/**********************************************************/
/*  Name: DISP.C                                          */
/*  Implementation: Using the command line parameter,     */
/*           display a single file to the screen          */
/*  Syntax:  DISP filename                                */
/*  Notes:   This program contains limited error checking */
/*           Additional error checking is left to you     */
/**********************************************************/

#include <stdio.h>
#define  MAXLINE  256
int main(int argc, char *argv[])
{
    FILE *st;           // Stream pointer
    int  rtn = 0;       // Assume good return
    char buf[MAXLINE];  // Buffer to read the file
    
    if (argc == 2)      // test for a command line parameter
    {                   // assume it is a file name
        st = fopen(argv[1], "r");
        if (st == NULL)
        {
            printf("File not found: %s \n", argv[1]);
            rtn = 1;    // Exit with error
        }
        else
        {    
            while (fgets(buf, MAXLINE, st) != NULL)
            {
                printf("%s", buf);
            }
            close(st);
        }
    }
    else
    {
        printf("Syntax:           \n");
        printf("    DISP filename \n");
        rtn = 2;        // Exit with error
    }
    return rtn;
}

Now give it a try with the command

Generic Code Example:

d:\GIDNetwork\>DISP disp.c

This version will only run with a single file. Use the concepts from arg.c to add the ability to display more than one file. And with a little additional thought, if no parameters are given, ask the user for a file name.

Next, we'll look at individual character and numeric parameters.

Would you like to comment? This story has been viewed 22,256 times.
« PHP function to ping Google Sitemaps Beginning Python Tutorial (Part 4) »

__top__

Copyright © GIDNetwork™ 2001 - 2024

Another website by J de Silva

Page generated in : 0.00579 sec.