簡體   English   中英

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

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

我得到這樣的系統時間:

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

然后在屏幕上顯示:

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

但是安裝了當前時間:12 : 57 :56我得到了: 0x001967a8在屏幕上。 我做錯了什么?

您正在嘗試打印“寬”字符串。 您需要使用:

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

“窄”版本的cout不知道“寬”字符,因此只會打印地址,就像您嘗試打印其他隨機指針類型一樣。

嘗試:

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

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