简体   繁体   English

std :: cout在屏幕上显示十六进制而不是文本

[英]std::cout shows hex on the screen instead of text

I get system time like this: 我得到这样的系统时间:

time_t t = time(0); 
struct tm* now = localtime(&t);
TCHAR* tInfo = new TCHAR[256];
swprintf_s(tInfo
    , 256
    , _T("Current time: %i:%i:%i")
    , now->tm_hour
    , now->tm_min
    , now->tm_sec);

And then show on screen: 然后在屏幕上显示:

std::cout << tInfo << std::endl; 

But insted of Current time: 12:57:56 I got: 0x001967a8 on the screen. 但是安装了当前时间:12 : 57 :56我得到了: 0x001967a8在屏幕上。 What I did wrong ? 我做错了什么?

You are trying to print a "wide" string. 您正在尝试打印“宽”字符串。 You need to use : 您需要使用:

std::wcout << tInfo << std::endl;

The "narrow" version cout doesn't know about "wide" characters, so will just print the address, just like if you tried to print some other random pointer type. “窄”版本的cout不知道“宽”字符,因此只会打印地址,就像您尝试打印其他随机指针类型一样。

尝试:

std::wcout << tInfo << std::endl; 

C++ shares its date/time functions with C. The tm structure is probably the easiest for a C++ programmer to work with - the following prints today's date: C ++与C共享其日期/时间功能。tm结构可能是C ++程序员最容易使用的-以下是今天的日期:

#include <ctime>
#include <iostream>
using namespace std;

int main() {
time_t t = time(0);   // get time now
struct tm * now = localtime( & t );
cout << (now->tm_year + 1900) << '-' 
     << (now->tm_mon + 1) << '-'
     <<  now->tm_mday
     << endl;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM