arguments to main()

I have a header file & implementation file called by a file containing my main function. Once I build and run my main function, I want the user to call the executable with one argument only.

hearder file: FWI.h
implementation file: FWI.cpp
main: FWI_main.cpp

in main I have:
1
2
3
int main(char *file_ptr){
...
}


I know i can use argc & argv, but I feel this is more than what I want (i.e. why should I make the user create and array with one element? just pass the one element-)

QUESTIONS:
1. Is it possible to do what I have?
2. I am on a linux, from cmdline after I have the executable made, ./FWI_mian(ptr), how do i define ptr at the cmdline?
You seem to have a very wrong concept of what exactly argc is.

To pass arguments to a program a user does this:
program argument1 argument2 ...
The shell will interpret the command line however it wants and create an array out of that. Assuming program is found, this array will eventually get passed to its main().
The shell will pass at least two things to the program: the command that called the program, and a string with all the arguments. Some shells (Bash) will parse the arguments and pass them directly to the program, while others (Windows) will simply pass the argument string unprocessed and leave interpretation up to the program (in console programs, this happens before main() is called, so don't worry about it). Regardless, you can't restrict how many arguments are passed, except by quitting when the wrong number of arguments is found.
that was quite wordy with different verbiage than most of what's posted out there regarding this. Let me try and bridge the gap-

hearder file: FWI.h
implementation file: FWI.cpp
main: FWI_main.cpp

in main:
1
2
3
4
int main(int argc,char *argv[]){
    ...
    return 0;
}


From command line after making th executable:

./FWI_main "string i wish to pass" "another string i wish to pass"

useful link I just found: http://www.crasseux.com/books/ctutorial/argc-and-argv.html

How do you write the code to quit with the wrong # of arguments?
Last edited on
if (argc!=right_number_of_arguments) return 1;
THANKS- and for anyone with the same question, for a really good tutorial go to: http://www.crasseux.com/books/ctutorial/argc-and-argv.html
main is an unusual function.
It is called by the operating system, and as C++ can run on many systems and the C++ standards cannot dictate to OS writers what data they pass to programs running on that OS - you
can write any parameters you like in the main functions and the compiler will accept it.

For example:
The following is meaningless but will compile with no problem:
1
2
3
4
int main(char* pc, float g)
{
    return 0;
} 


IIRC, the C++ standards only stipulate the following concerning parameters to and from main

1. main must return int
2. The follwing prototypes must be honoured by the compiler
1
2
int main(int, char *[])
       int main (int  char**)

3. if there is no explicit return statement - then 0 is automatically returned.
Since you only want to pass one argument, what I'm about to say is a bit redundant if applied here, but it would be a good idea in general to have the command line parsing wrapped in a class:

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
using namespace std;

class CLParser
{
public:

    CLParser(int argc_, char * argv_[],bool switches_on_=false);
    ~CLParser(){}

    string get_arg(int i);
    string get_arg(string s);

private:

    int argc;
    vector<string> argv;

    bool switches_on;
    map<string,string> switch_map;
};

CLParser::CLParser(int argc_, char * argv_[],bool switches_on_)
{
    argc=argc_;
    argv.resize(argc);
    copy(argv_,argv_+argc,argv.begin());
    switches_on=switches_on_;

    //map the switches to the actual
    //arguments if necessary
    if (switches_on)
    {
        vector<string>::iterator it1,it2;
        it1=argv.begin();
        it2=it1+1;

        while (true)
        {
            if (it1==argv.end()) break;
            if (it2==argv.end()) break;

            if ((*it1)[0]=='-')
                switch_map[*it1]=*(it2);

            it1++;
            it2++;
        }
    }
}

string CLParser::get_arg(int i)
{
    if (i>=0&&i<argc)
        return argv[i];

    return "";
}

string CLParser::get_arg(string s)
{
    if (!switches_on) return "";

    if (switch_map.find(s)!=switch_map.end())
        return switch_map[s];

    return "";
}

//////////////////////////////////////////////////////////////

int main(int argc, char * argv[])
{
    CLParser cmd_line(argc,argv,true);

    cout << "printing the whole arg list...\n" << endl;
    for (int i=0; i<argc; i++)
        cout << cmd_line.get_arg(i) << endl;

    cout << "\nprinting the switch values...\n" << endl;

    string temp;

    temp=cmd_line.get_arg("-a");
    if (temp!="") cout << temp << endl;

    temp=cmd_line.get_arg("-b");
    if (temp!="") cout << temp << endl;

    temp=cmd_line.get_arg("-c");
    if (temp!="") cout << temp << endl;

    cout << "\nhit enter to quit";
    cin.get();
    return 0;
}

A call like this one:

cl_parser

generates this output:

printing the whole arg list...

cl_parser

printing the switch values...


hit enter to quit

While a call like this:

cl_parser -c asdf -b qwerty -a "1 2 3 4 5"

generates this:

printing the whole arg list...

cl_parser
-c
asdf
-b
qwerty
-a
1 2 3 4 5

printing the switch values...

1 2 3 4 5
qwerty
asdf

hit enter to quit
Last edited on
Yes... I was wondering if jsmith would pop up with a link like this... But it was you instead... Anyway, I just wanted to demonstrate a simple implementation for handling switches. I don't doubt that boost does it in a more elegant way (and neither do I doubt that I could do it too if I spent more time on it).
Topic archived. No new replies allowed.