ARGV [1]包含在Visual C ++ 2010的第一个命令行参数的第一个字母 [英] argv[1] contains only the first letter of the first command-line argument in Visual C++ 2010
问题描述
可能重复:结果
之间的区别
<一href=\"http://stackoverflow.com/questions/895827/what-is-the-difference-between-tmain-and-main-in-c\">What在C _tmain()和main()的++?
块引用>我有一个控制台应用程序,这需要一个命令行参数。
INT _tmain(INT ARGC,CHAR *的argv [])
{
...
的printf(路径:%S \\ N的argv [1]);
...
}当我带参数运行程序(
myprogram.exe D:\\ MyFolder文件\\ MYFILE
),它打印路径:D $ C $的c>来代替
路径:D:\\ MyFolder文件\\ MYFILE
我应该怎么改程序提取第一个命令行参数(
的argv [1]
),以便它返回的完整路径,而不只是第一个字母<? / p>我试图把在引号的路径。(
myprogram.exeD:\\ MyFolder文件\\ MYFILE
),但它并没有帮助解决方案使用
_tprintf
而不是的printf
。你的程序是几乎可以肯定设置编译为UNI code
,因此,每一个性格占两个字节。您拨打
的printf
这在单字节字符运行。第二个字节恰好是0,因此的printf
认为它达到了字符串的结尾,因为一个零字节被视为一个字符串的结束。,并确保您
的argv
声明为TCHAR
,而不是字符
。是一致的:要么使用TCHAR
程序,或没有。但是,不要混用,除非你有一个非常很好的理由(即你知道你在做什么,你都在摩拳擦掌做一些魔法)。机会是没有这样做,你不应该。Possible Duplicate:
What is the difference between _tmain() and main() in C++?I have a console application, which takes one command line parameter.
int _tmain(int argc, char* argv[]) { ... printf("Path: %s\n", argv[1]); ... }
When I run the program with an argument (
myprogram.exe D:\myfolder\myfile
), it printsPath: D
instead ofPath: D:\myfolder\myfile
.How should I change the procedure for extracting first command-line parameter (
argv[1]
) so that it returns the full path, not just the first letter?I tried to put the path in quotes (
myprogram.exe "D:\myfolder\myfile"
), but it didn't help.解决方案Use
_tprintf
instead ofprintf
. Your program is almost certainly set to compile asUNICODE
and, therefore, each "character" takes up two bytes.You call
printf
which operates on single byte characters. The second byte happens to be 0, and soprintf
thinks it reached the end of the string since a zero byte is considered the termination of a string.And make sure your
argv
is declared asTCHAR
and notchar
. Be consistent: either use theTCHAR
routines, or don't. But don't mix unless you have a very good reason (i.e. you know what you're doing and you are gearing up to do some magic). Chances are you don't and you shouldn't.这篇关于ARGV [1]包含在Visual C ++ 2010的第一个命令行参数的第一个字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!