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

查看:115
本文介绍了无法在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;
}

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

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构建中为char*,因此,除了获得特殊的字符串行为之外,还获得了默认的指针行为.

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天全站免登陆