Reputation: 1457
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. What I did wrong ?
Upvotes: 0
Views: 246
Reputation: 6590
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:
#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;
}
Upvotes: 0
Reputation: 129324
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.
Upvotes: 4