如何写出带执行参数的程序
如何写出带执行参数的程序啊,可能有人没懂我意思,我先解释下。
比如你可以在运行或命令提示符内输入notepad.exe new.txt,就可以打开C盘目录下new.txt文件,如果没有记事本程序就提示新建一个
再比如在Chrome浏览器,在其桌面快捷方式,选择-"属性"-"快捷方式",然后在"目标"一栏尾部添加参数 --enable-easy-off-store-extension-install ,然后再运行浏览器就可以像以前那样正常安装 Web Store 之外的第三方扩展应用及脚本程序了。
希望有人能用C或C++写出一个小例子,让我参考下,比如写一个两数相加的简单命令行程序,在命令行输入Add.exe 5, 4就能输出结果9的。
[解决办法]
int main(int argc,char* argv[])
如果程序名字是Test.exe
那么执行命令 Test.exe 1 2 3 -enable
argc = 5
argv[0]=Test.exe
argv[1]=1
argv[2]=2
argv[3]=3
argv[4]=-enable
[解决办法]
楼主是你没看懂1楼的意思吧
Test.exe 1 2 3 -enable 就是执行的时候输入的,命令行啊
不过我一直用GetCommandLine()来获取输入参数的
[解决办法]
如果是console,则象一楼那样
如果是mfc,则本身就可以带参数了,但参数得自己解析
[解决办法]
main, wmain
main( int argc, char *argv[ ], char *envp[ ] )
{
program-statements
}
wmain( int argc, wchar_t *argv[ ], wchar_t *envp[ ] )
{
program-statements
}
The main function marks the beginning and end of program execution. A C or C++ program must have one function named main. If your code adheres to the Unicode programming model, you can use the wide-character version of main, which is wmain.
The main and wmain functions can take the following three optional arguments, traditionally called argc, argv, and envp (in that order):
argc
An integer specifying how many arguments are passed to the program from the command line. Because the program name is considered an argument, argc is at least 1.
argv
An array of null-terminated strings. It can be declared as an array of pointers to char (char *argv[ ] or wchar_t *argv[ ] for wmain) or as a pointer to pointers to char (char **argv or wchar_t **argv for wmain). The first string (argv[0]) is the program name, and each following string is an argument passed to the program from the command line. The last pointer (argv[argc]) is NULL.
[解决办法]
GetCommandLine() CommandLineToArgvW()自己解析命令行参数