setting up char** argv in code

送分小仙女□ 提交于 2019-12-23 05:31:15

问题


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


回答1:


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

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



回答2:


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.



来源:https://stackoverflow.com/questions/12915669/setting-up-char-argv-in-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!