无法在 VC++ 中使用 std::cout 打印出 argv[] 值
这是我在该网站上的第一个问题,尽管我来这里参考已经有一段时间了。我知道 argv[0] 存储程序的名称,其余命令行参数存储在剩余的 argv[k] 槽中。我还了解 std::cout 将字符指针视为空终止字符串并打印该字符串。下面是我的程序。
#include "stdafx.h"
#include <fstream>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout << argv[0] << " ";
cout << argv[1] ;
return 0;
}
根据我在本期互联网搜索中看到的所有其他程序,该程序应该打印出两个字符串,即。程序名称和命令行参数。控制台窗口显示
0010418c 001048d6
我相信这些是分别指向 argv[0] 和 argv[1] 的指针。 我唯一的命令行争论是“nanddumpgood.bin”,它出现在 argv[1] 中,如果我在调试时将鼠标悬停在 argv[] 数组上,它会正确显示字符串。
这是什么情况?我做错了什么?我明白,在特殊情况下数组会衰减为指针?这是没有的情况吗?
This is my first question on the site even though i have been coming here for reference for quite some time now. I understand that argv[0] stores the name of the program and the rest of the commandline arguements are stored in teh remaining argv[k] slots. I also understand that std::cout treats a character pointer like a null terminated string and prints the string out. Below is my program.
#include "stdafx.h"
#include <fstream>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout << argv[0] << " ";
cout << argv[1] ;
return 0;
}
According to all the other programs I have seen over my internet search in the issue, this program should printout two strings viz. name of the program and the commandline arguement. The console window shows
0010418c 001048d6
I believe these are the pointers to argv[0] and argv[1] resp.
The only commandline arguement I have is "nanddumpgood.bin" which goes in argv[1] and shows the strings correctly if I mouseover the argv[] arrays while debugging.
Whis is this happening? What am I doing wrong? I understand, arrays decay to pointers in special cases? Is this a case where it doesnt?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这大部分是正确的。它适用于
char*
,但不适用于其他类型的字符。这正是问题所在。您有一个_TCHAR*
,它在 ANSI 版本上是char*
,但在 Unicode 版本上不是,因此您不会获得特殊的字符串行为,而是获得默认的指针行为。argv
是一个数组,但argv[0]
和argv[1]
都不是数组,它们都是指针。腐烂不是这里的一个因素。最简单的修复方法是使用 int main(int argc, char* argv[]) 以便获得命令行参数的非 Unicode 字符串。我建议这样做,而不是切换到
wcout
,因为它与您在互联网上找到的其他代码更兼容。That's mostly correct. It works for
char*
, but not other types of characters. Which is exactly the problem. You have a_TCHAR*
, which ISchar*
on an ANSI build but not on a Unicode build, so instead of getting the special string behavior, you get the default pointer behavior.argv
is an array, but neitherargv[0]
norargv[1]
are arrays, they are both pointers. Decay is not a factor here.The simplest fix is to use
int main(int argc, char* argv[])
so that you get non-Unicode strings for the command-line arguments. I'm recommending this, rather than switching towcout
, because it's much more compatible with other code you find on the internet.对 Unicode 字符串使用
wcout
。Use
wcout
for Unicode strings.我猜您正在使用 unicode 编译器开关来编译您的应用程序,该开关将所有 TCHAR 视为 wchar_t。因此,cout 将 argv 视为 int。
改为写入
或更改为“项目设置/常规”中的
使用多字节字符
集。I guess you are compiling your application with the unicode compiler switch which treats all TCHAR as wchar_t. Therefore cout treats argv as an int.
Write instead
or change to
Use Multi-byte character
set in the Project settings/General.