char * argv []中的内容与期望不一致 [英] The content in char* argv[] is not consistent with expectation

查看:49
本文介绍了char * argv []中的内容与期望不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究如何利用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!

推荐答案

_tmain argv 参数>不是类型 char * 但是 _tchar * 。使用Unicode构建时,这将是一个宽字符,每个字符占用两个字节。对于ASCII字符,第一个(低)字节是ASCII码,第二个(高)字节是零。



另见MSDN [ ^ ]。
The argv parameter of _tmain is not of type char* but _tchar*. With Unicode builds this will be a wide character occupying two bytes per character. With ASCII characters the first (low) byte is the ASCII code and the second (high) byte is zero.

See also the MSDN[^].


首先关闭所有特定于MS的垃圾,直到您确定所有内容都按照标准C ++中的标准进行操作。如果你真的想要使用C ++,那么除了一个C程序的构造之外。



所以尝试类似:



First off bin all the MS specific rubbish until you're sure that everything works as advertised in standard C++. That's if you really want to use C++ as aside from one construct that's a C program.

So try something like:

#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 []中的内容与期望不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆