Command Line Arguments

closed account (Lv0f92yv)
Hello, I am writing a simple program to handle some command line arguments.

I am using Fedora 12 and Eclipse (using GCC compiler).

When I type ./programname in the terminal with no more arguments, the first argument (argv[0]) listed in the output is: ./programname. Why is this?

Also, does the operating system handle the details regarding how many arguments are passed? Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

using namespace std;

int main( int argc, char* argv[], char* envp[])
{
	cout << "Command Line Parser\n";
	// Display each command-line argument.
	cout << "\nCommand-line arguments:\n";
	for( int count = 0; count < argc; count++ )
	     cout << "  argv[" << count << "]\n   "
	            << argv[count] << "\n";


	return 0;
}


This snippet was taken from Microsoft's tutorial on command line parsing, and I am using it as a reference for further development.

Thanks in advance for any replies. I will be happy to clarify if necassary.
Last edited on
argv isn't really the "arguments" it's more like the entire command line. Thus, argv[0] is the first thing on the command line (the name you typed in to run it). As for the second question, I'm not really sure what you mean.
closed account (Lv0f92yv)
I see. Thanks. For the second question, I am referring to the first and last parameters in the main method: argc and envp[]. Where/by whome are these initialized? My guess is the OS, but I am not sure. For example, I am using argc to determine how many arguments are on the command line. How does it know?
First, that is not a standard signature for main. These are standard:
1
2
int main() { /*...*/ }
int main( int argc, char * args[] ) { /*...*/ }

More information can be found here: http://www2.research.att.com/~bs/bs_faq2.html#void-main

Second, it's not really a question of how the shell counted the command line arguments but more of why. The argument count is really just the size length of the args array.
Last edited on
closed account (Lv0f92yv)
I see. Thanks for the reply.
The argc is very handy, also stops you running into access violations if you try to access say argv[8] which doesn't exist. in this case would throw a stackdump.

take this piece of code for example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int main(int argc,char *argv[])
{
    string sw=""
    int row=1;
    string inputFile="";
    string outputFile="";
    string usage=" Usage: -i input_filename  -o output_filename";

    while ( row < argc )
    {
        sw = argv[row];
        if ( sw == "-i" )
        {
            row++;
            if ( row < argc )
                inputFile = argv[row];
        }
        else if ( sw == "-o" )
        {
            row++;
            if ( row < argc )
                outputFile = argv[row];
        }
        else
        {
            cerr << "Unknown switch: " << sw << endl;
            cerr << usage;
            exit (1);
        }
    
        row++;
    }
}
            


I'm always checking to make sure that there's more arguments left than my current count, while still being able to parse almost infinite number of command line args. That way I don't try and access an element in argv[n] that doesn't exist.
Last edited on
closed account (Lv0f92yv)
Ahh I see. This makes a lot of sense. Thanks. What about the envp variable though? (Microsoft calls this an 'environment' variable), but I am not sure what relevance it holds. I checked out the link provided by moorecmm, and it is not listed as a 'standard' parameter.

Just ignore it.. it's non standard so there's no point using it unless you have been told to.
Being that you are using fedora it doesn't make much sense why you'd use a nonstandard, or something that would only compile on windows.
Last edited on
Topic archived. No new replies allowed.