无法在 VC++ 中使用 std::cout 打印出 argv[] 值 [英] Can not print out the argv[] values using std::cout in VC++

查看:18
本文介绍了无法在 VC++ 中使用 std::cout 打印出 argv[] 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在该网站上的第一个问题,尽管我已经来这里供参考已有一段时间了.我知道 argv[0] 存储程序的名称,其余的命令行参数存储在剩余的 argv[k] 插槽中.我也明白 std::cout 将字符指针视为空终止字符串并将字符串打印出来.下面是我的程序.

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;
}

根据我在 Internet 搜索中看到的所有其他程序,该程序应该打印出两个字符串,即.程序名称和命令行参数.控制台窗口显示

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

0010418c 001048d6

我相信这些是指向 argv[0] 和 argv[1] 的指针.我唯一的命令行参数是nanddumpgood.bin",它进入 argv[1] 并正确显示字符串,如果我在调试时将鼠标悬停在 argv[] 数组上.

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?

推荐答案

我也明白 std::cout 将字符指针视为空终止字符串并将字符串打印出来.

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

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

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 是一个数组,但 argv[0]argv[1] 都不是数组,它们都是指针.衰减不是这里的一个因素.

argv is an array, but neither argv[0] nor argv[1] are arrays, they are both pointers. Decay is not a factor here.

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

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.

这篇关于无法在 VC++ 中使用 std::cout 打印出 argv[] 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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