【发布时间】:2014-02-03 11:33:07
【问题描述】:
我需要帮助,因为我在尝试读取命令行参数时没有得到预期的输出。这真的很奇怪,因为我将代码复制并粘贴到常规控制台应用程序中,它按预期工作。值得注意的是,我正在运行 Windows 7,在 Visual Studio 中我将命令行参数设置为 test.png
Win32 代码:
#include "stdafx.h"
using namespace std;
int _tmain(int argc, char* argv[])
{
//Questions: why doesn't this work (but the one in helloworld does)
//What are object files? In unix I can execute using ./ but here I need to go to debug in top directory and execute the .exe
printf("hello\n");
printf("First argument: %s\n", argv[0]);
printf("Second argument: %s\n", argv[1]);
int i;
scanf("%d", &i);
return 0;
}
输出:
hello
First Argument: C
Second Argument: t
我尝试创建一个简单的控制台应用程序,它可以工作:
#include <iostream>
using namespace std;
int main(int arg, char* argv[])
{
printf("hello\n");
printf("First argument: %s\n", argv[0]);
printf("Second argument: %s\n", argv[1]);
int i;
scanf("%d", &i);
return 0;
}
输出:
hello
First Argument: path/to/hello_world.exe
Second Argument: test.png
有人知道发生了什么吗?
【问题讨论】:
-
我猜我会说你有一个 Unicode 版本,但正在尝试打印字符串,就好像它们是 Ansi 一样。
-
@JonathanPotter that 是如何发生并仍然链接的?还是首先是这个问题(没有建立)?我同意你的看法,我只是不知道它是如何构建的。奇怪。
-
@WhozCraig:我也是,虽然不能保证这里显示的代码是实际上正在编译的代码。
-
它正在编译中...我没有做任何重大的事情。这些是visual studio 2013提供的模板。我只是在里面写了printf。
-
@JonathanPotter 像往常一样,很好。
标签: c++ winapi command-line-arguments