从argv获取字符 [英] Getting chars from argv
问题描述
该应用程序是Windows 7企业版,MFC,Visual Studio 2008,C ++,启用了unicode
所需的参数是单个字符。 VS配置为提供命令行参数,值为1,argc在添加命令行参数后从1增加到2.
The app is Windows 7 Enterprise, MFC, Visual Studio 2008, C++, unicode enabled
The argument needed is a single character. VS is configured to provide command line argument, value is "1", argc has incremented from 1 to 2 after adding the command line argument.
char str_number = __argv[ 1 ];
编译错误无法从char *转换为char,很酷,理解
Compile error cannot convert from char* to char, cool, understood
char str_number = *__argv[ 1 ];
访问冲突读取位置0x00000004
Access violation reading location 0x00000004
char str_number = *__argv[ 1,0];
访问冲突读取位置0x00000000
我尝试了什么:
我尝试过块中显示的示例:描述问题。
Access violation reading location 0x00000000
What I have tried:
I have tried the examples shown in block: Describe the problem.
推荐答案
在尝试引用任何__ argc
的值> __ argv 元素。类似于:
You first need to check the value of__argc
before attempting to refer to any of the__argv
elements. Something like:
if (__argc > 1)
char str_number = *__argv[ 1 ];
或者您可以使用 CCommandLineInfo类 [ ^ ]。
两者
Both
char str_number = *__argv[1];
和
and
char str_number = __argv[1][0];
应该有效。
请在访问 __ argv
之前,始终检查 __ argc
值,如 Richard 所述。
should work.
Please check always __argc
value before accessing __argv
, as pointed out by Richard.
当您的应用程序启用了unicode时,您应该使用__wargv而不是__argv。再次尝试你的第二个例子,你应该很幸运。
When your application is unicode enabled you should use __wargv instead of __argv. Try your second example again with that change and you should be in luck.
这篇关于从argv获取字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!