ARGV [1]包含在Visual C ++ 2010的第一个命令行参数的第一个字母 [英] argv[1] contains only the first letter of the first command-line argument in Visual C++ 2010

查看:333
本文介绍了ARGV [1]包含在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 来代替路径: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 prints Path: D instead of Path: 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 of printf. Your program is almost certainly set to compile as UNICODE 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 so printf 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 as TCHAR and not char. Be consistent: either use the TCHAR 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屋!

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