问题描述
我正在研究如何利用argc和argv []参数在WIN8.1 x64系统的命令窗口中编写便利UI。这是我在VS2013 c ++中的源代码:
I am studying how to utilize argc and argv[] parameters to program a convenience UI in Command windows in WIN8.1 x64 system. Here's my source code in VS2013 c++:
int _tmain(_In_ int argc, _In_reads_(argc) char* argv[])
{
PCHAR parm;
printf("argc in main is %d\n", argc);
for (int i = 0; i < argc; i++){
parm = argv[i];
printf("&argv[%d] = %x\n", i, &argv[i]);
printf("argv[%d] = %x\n", i, argv[i]);
printf("*argv[%d] = %c\n", i, *argv[i]);
printf("parm[0] = %c, parm[1] = %c, parm[2] = %c \n", parm[0], parm[1], parm[2]);
}
return 0;
}
我们假设exe文件是test.exe。当我在命令窗口输入
test.exe / a
时,我可以得到argc = 2.当我要打印时输出存储在argv []中的值,发生了奇怪的事情。据说我应该得到如下输出:
& argv [0] = b76148
argv [0] = b76154
* argv [0] = t
parm [0] = t,parm [1] = e,parm [2] = s
// test.exe的前3个CHAR
& argv [0] = b7614c
argv [0] = b76184
* argv [0] = /
parm [0] = /,parm [1] = a,parm [2] =
//前3个CHAR / a
但是,我实际得到以下内容:
& argv [0] = b76148
argv [0] = b76154
* argv [0] = t
parm [0] = t,parm [1] =,parm [2] = e
& ; argv [0] = b7614c
argv [0] = b76184
* argv [0] = /
parm [0] = / ,parm [1] =,parm [2] = a
因为parm是PCHAR,据说我可以打印出存储在agrv中的字符[ ]一个接一个,但为什么parm [1]总是零,parm [2]是CHAR在parm [0]旁边。我想这是一个内存大小问题,但是当sizeof(CHAR)= 1时,我无法弄清楚为什么第一个CHAR的内存位置距离第二个CHAR 2个字节。
感谢患者查看此问题&感谢您的帮助,告诉我为什么我错了。谢谢大家!
Let's assume the exe file is test.exe. While I type in
"test.exe /a"
in command window, I can get argc = 2. When I want to print out the valued stored in argv[], the weird thing happened. Supposedly I should get the output like the following:
&argv[0] = b76148
argv[0] = b76154
*argv[0] = t
parm[0] = t, parm[1] = e, parm[2] = s
// first 3 CHAR of test.exe
&argv[0] = b7614c
argv[0] = b76184
*argv[0] = /
parm[0] = /, parm[1] = a, parm[2] =
// first 3 CHAR of /a
However, I get the following in actual:
&argv[0] = b76148
argv[0] = b76154
*argv[0] = t
parm[0] = t, parm[1] = , parm[2] = e
&argv[0] = b7614c
argv[0] = b76184
*argv[0] = /
parm[0] = /, parm[1] = , parm[2] = a
Because parm is a PCHAR, supposedly I can print out the characters stored in agrv[] one by one, but why parm[1] always be zeros and parm[2] is the CHAR next to parm[0]. I guess it is a memory size issue, but while sizeof(CHAR)=1, I can't figure out why the memory location of 1st CHAR is 2 bytes away from 2nd CHAR.
Thanks for the patient to view this question & I appreciate your help to tell me why I am wrong. Thanks all!
推荐答案
#include <algorithm>
#include <iostream>
#include <iterator>
int main( int argc, char *argv[] )
{
std::copy( argv, argv + argc, std::ostream_iterator<const char *>( std::cout, "\n" ) );
}
这是标准的C ++'03,它将显示传递给程序的命令行参数。你可以从那里拿走......
which is standard C++'03 and will display the command line parameters you pass to the program. You can take it from there...
这篇关于char * argv []中的内容与期望不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!