Murphy316
Murphy316

Reputation: 766

setting up char** argv in code

So I have a program called prog.exe that starts with

int main(int argc, char** argv)

The parameters that I pass to it are

prog.exe news-rec

or prog.exe news-rec -t -p

How could I configure the values of these parameters in code so that I dont have to run the program from command line.

For example for

prog.exe news-rec

I think argc = 1 but how will I configure argv what will it be? I tried doing

argv[0] = "news-rec";

but that doesn't work

Upvotes: 0

Views: 1746

Answers (2)

Abhineet
Abhineet

Reputation: 5389

You do not need to give the prog.exe in your command-line argument as the exe name is always the argv[0] i.e. the first argument. Other than that, you can store as many arguments in argv as you wish, ex::

argv[1] = "new-rec" ;
argv[2] = "-t" ;
argv[3] = "-p" ;

and so on...

Assuming from the tag, that you are working on Visual Studio, you can try::

Project Tab-> Properties-> Configuration Properties-> Debugging and then enter in the Right Hand Side under (Command Arguments), the arguments you want to pass excluding the prog.exe. The argc will automatically take the count of number of arguments you have passed.

Upvotes: 2

user529758
user529758

Reputation:

Because argv[0] contains the name of the executable itself. Try

argc = 2;
argv[1] = "news-recc";

Upvotes: 3

Related Questions