无法在 VC++ 中使用 std::cout 打印出 argv[] 值

发布于 2024-12-15 13:34:24 字数 637 浏览 5 评论 0原文

这是我在该网站上的第一个问题,尽管我来这里参考已经有一段时间了。我知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生来就爱笑 2024-12-22 13:34:24

我还了解 std::cout 将字符指针视为空终止字符串并打印该字符串。

大部分是正确的。它适用于 char*,但不适用于其他类型的字符。这正是问题所在。您有一个 _TCHAR*,它在 ANSI 版本上是 char*,但在 Unicode 版本上不是,因此您不会获得特殊的字符串行为,而是获得默认的指针行为。

我明白,在特殊情况下数组会衰减为指针?这是不是的情况?

argv 是一个数组,但 argv[0]argv[1] 都不是数组,它们都是指针。腐烂不是这里的一个因素。

最简单的修复方法是使用 int main(int argc, char* argv[]) 以便获得命令行参数的非 Unicode 字符串。我建议这样做,而不是切换到 wcout,因为它与您在互联网上找到的其他代码更兼容。

I also understand that std::cout treats a character pointer like a null terminated string and prints the string out.

That's mostly correct. It works for char*, but not other types of characters. Which is exactly the problem. You have a _TCHAR*, which IS char* on an ANSI build but not on a Unicode build, so instead of getting the special string behavior, you get the default pointer behavior.

I understand, arrays decay to pointers in special cases? Is this a case where it doesnt?

argv is an array, but neither argv[0] nor argv[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 to wcout, because it's much more compatible with other code you find on the internet.

以可爱出名 2024-12-22 13:34:24

对 Unicode 字符串使用 wcout

Use wcout for Unicode strings.

妄想挽回 2024-12-22 13:34:24

我猜您正在使用 unicode 编译器开关来编译您的应用程序,该开关将所有 TCHAR 视为 wchar_t。因此,cout 将 argv 视为 int。

改为写入

wcout << argv[0] << L" "; 
wcout << argv[1] ;

或更改为“项目设置/常规”中的使用多字节字符集。

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

wcout << argv[0] << L" "; 
wcout << argv[1] ;

or change to Use Multi-byte character set in the Project settings/General.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文