So I'm working with time.h for this (copied from msdn):

int main( void )
        struct tm newtime;
        char am_pm[] = "AM";
        __time64_t long_time;
        char timebuf[26];
        errno_t err;

        // Get time as 64-bit integer.
        _time64( &long_time ); 
        // Convert to local time.
        err = _localtime64_s( &newtime, &long_time ); 
        if (err)
            printf("Invalid argument to _localtime64_s.");
        if( newtime.tm_hour > 12 )        // Set up extension. 
                strcpy_s( am_pm, sizeof(am_pm), "PM" );
        if( newtime.tm_hour > 12 )        // Convert from 24-hour 
                newtime.tm_hour -= 12;    // to 12-hour clock. 
        if( newtime.tm_hour == 0 )        // Set hour to 12 if midnight.
                newtime.tm_hour = 12;

        // Convert to an ASCII representation. 
        err = asctime_s(timebuf, 26, &newtime);
        if (err)
           printf("Invalid argument to asctime_s.");
        printf( "%.19s %s\n", timebuf, am_pm );

when i notice the printf lines. Well, ive been using cout this whole project and wanted to know if anyone can help me with a switch-over for it.

1 Year
Discussion Span
Last Post by triumphost

I assume you want get rid of printf and use cout instead?
cout works very similar to printf, except the semantics are slightly different

cout << "string" << (whatever integer you are using) << endl;//if you want to create a new line

The << is like the , in printf.
I did a poor explanation, but hopefully this helps clarify things a bit.


cout cannot "replace" printf because printf has formatting flags.. cout does not.

You would have to use a modifier or sprintf to a buffer then print the buffer using cout.

Modifiers such as std::setprecision would suffice. If you are sure you only want to print 19 characters max, then std::cout.write(time_buff, 19); would also do.

Edited by triumphost

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.