C++ 读取Win32控制台应用程序中的命令行参数时出错?
我需要帮助,因为我在尝试读取命令行参数时没有得到预期的输出。这真的很奇怪,因为我将代码复制并粘贴到了一个常规的控制台应用程序中,并且它按预期工作。值得注意的是,我正在运行Windows 7,并且在visual studio中将命令行参数设置为test.png Win32代码:C++ 读取Win32控制台应用程序中的命令行参数时出错?,c++,winapi,command-line-arguments,C++,Winapi,Command Line Arguments,我需要帮助,因为我在尝试读取命令行参数时没有得到预期的输出。这真的很奇怪,因为我将代码复制并粘贴到了一个常规的控制台应用程序中,并且它按预期工作。值得注意的是,我正在运行Windows 7,并且在visual studio中将命令行参数设置为test.png Win32代码: #include "stdafx.h" using namespace std; int _tmain(int argc, char* argv[]) { //Questions: why doesn't th
#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
hello
First Argument: path/to/hello_world.exe
Second Argument: test.png
我尝试创建一个简单的控制台应用程序,它可以工作:
#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;
}
有人知道发生了什么吗?
\u tmain
只是一个宏,它的变化取决于您是使用Unicode还是ASCII进行编译,如果是ASCII,它将放置main
,如果是Unicode,它将放置wmain
如果希望正确的Unicode声明接受Unicode格式的命令行参数,则必须将其声明为接受以下Unicode字符串:
intwmain(intargc,wchar_*argv[])代码>
你可以阅读更多关于它的内容
代码的另一个问题是printf
需要ASCII C样式的字符串,而不是Unicode。使用wprintf
或使用std::wcout
打印Unicode样式的字符串
#include <iostream>
using namespace std;
int wmain(int argc, wchar_t* 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
std::cout << "Hello\n";
std::wcout << "First argument: " << argv[0] << "\n";
std::wcout << "Second argument: " << argv[1] << "\n";
return 0;
}
#包括
使用名称空间std;
int wmain(int argc,wchar_t*argv[])
{
//问题:为什么这个不起作用(但helloworld中的那个起作用)
//什么是对象文件?在unix中,我可以使用./执行,但这里我需要转到顶层目录中的debug并执行.exe
std::cout\u tmain
只是一个宏,它的变化取决于您是使用Unicode还是ASCII编译,如果它是ASCII,那么它将放置main
,如果它是Unicode,那么它将放置wmain
如果希望正确的Unicode声明接受Unicode格式的命令行参数,则必须将其声明为接受以下Unicode字符串:
intwmain(intargc,wchar_*argv[]);
你可以阅读更多关于它的内容
代码的另一个问题是printf
需要ASCII C样式字符串而不是Unicode。请使用wprintf
或使用std::wcout
打印Unicode样式字符串
#include <iostream>
using namespace std;
int wmain(int argc, wchar_t* 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
std::cout << "Hello\n";
std::wcout << "First argument: " << argv[0] << "\n";
std::wcout << "Second argument: " << argv[1] << "\n";
return 0;
}
#包括
使用名称空间std;
int wmain(int argc,wchar_t*argv[])
{
//问题:为什么这个不起作用(但helloworld中的那个起作用)
//什么是对象文件?在unix中,我可以使用./执行,但这里我需要转到顶层目录中的debug并执行.exe
std::cout\u tmain
只是一个宏,它的变化取决于您是使用Unicode还是ASCII编译,如果它是ASCII,那么它将放置main
,如果它是Unicode,那么它将放置wmain
如果希望正确的Unicode声明接受Unicode格式的命令行参数,则必须将其声明为接受以下Unicode字符串:
intwmain(intargc,wchar_*argv[]);
你可以阅读更多关于它的内容
代码的另一个问题是printf
需要ASCII C样式字符串而不是Unicode。请使用wprintf
或使用std::wcout
打印Unicode样式字符串
#include <iostream>
using namespace std;
int wmain(int argc, wchar_t* 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
std::cout << "Hello\n";
std::wcout << "First argument: " << argv[0] << "\n";
std::wcout << "Second argument: " << argv[1] << "\n";
return 0;
}
#包括
使用名称空间std;
int wmain(int argc,wchar_t*argv[])
{
//问题:为什么这个不起作用(但helloworld中的那个起作用)
//什么是对象文件?在unix中,我可以使用./执行,但这里我需要转到顶层目录中的debug并执行.exe
std::cout\u tmain
只是一个宏,它的变化取决于您是使用Unicode还是ASCII编译,如果它是ASCII,那么它将放置main
,如果它是Unicode,那么它将放置wmain
如果希望正确的Unicode声明接受Unicode格式的命令行参数,则必须将其声明为接受以下Unicode字符串:
intwmain(intargc,wchar_*argv[]);
你可以阅读更多关于它的内容
代码的另一个问题是printf
需要ASCII C样式字符串而不是Unicode。请使用wprintf
或使用std::wcout
打印Unicode样式字符串
#include <iostream>
using namespace std;
int wmain(int argc, wchar_t* 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
std::cout << "Hello\n";
std::wcout << "First argument: " << argv[0] << "\n";
std::wcout << "Second argument: " << argv[1] << "\n";
return 0;
}
#包括
使用名称空间std;
int wmain(int argc,wchar_t*argv[])
{
//问题:为什么这个不起作用(但helloworld中的那个起作用)
//什么是对象文件?在unix中,我可以使用./执行,但这里我需要转到顶层目录中的debug并执行.exe
std::cout At At a gust我猜你有一个Unicode版本,但正在试图打印字符串,就好像它们是Ansi一样。@JonathanPotter这是怎么发生的,并且仍然链接?或者这是一个首要问题(不构建)?我同意你的观点,我只是不知道它是如何构建的。奇怪。@WhozCraig:我也是,虽然这里显示的代码不能保证它实际上就是正在编译的代码。它正在编译…我没有做任何重要的事情。这些是visual studio 2013提供的模板。我实际上只是在里面编写了printf。@JonathanPotter像往常一样,excellent point。我猜您有一个Unicode版本,但正在尝试打印字符串,就好像它们是Ansi一样。@JonathanPotter这是怎么发生的,并且仍然链接?或者这是一个问题(不是版本)?我同意你的观点,我只是不知道它是如何构建的。奇怪。@WhozCraig:我也是,虽然这里显示的代码不能保证它实际上就是正在编译的代码。它正在编译…我没有做任何重要的事情。这些是visual studio 2013提供的模板。我实际上只是在里面编写了printf。@JonathanPotter像往常一样,excellent point。我猜您有一个Unicode版本,但正在尝试打印字符串,就好像它们是Ansi一样。@JonathanPotter这是怎么发生的,并且仍然链接?或者这是一个问题(不是版本)?我同意你的看法,我只是不知道它是如何构建的。奇怪。@WhozCraig:我也是,尽管这里显示的代码不能保证就是代码