I just wandered if there is a way to constantly update the time once it has been displayed on a console application. I am using the following code to display the time:

time_t rawtime;
	struct tm * timeinfo;

	time ( &rawtime );
	timeinfo = localtime ( &rawtime );
	printf ("The current time is %s\n", asctime (timeinfo));

Thanks in advance for any help,
climberboy

put that in its own thread so that other processing can be done. In the thread put the code in an infinite loop. Call Sleep(1000) inside the loop so that the thread waiks up only once per second. Also you might want to add '\r' at the beginning of the string. printf("\rCurrent ..."); Hopefully there is no other output in the program because if there is then '\r' won't work because the cursor may be on some other line. You could, nowever, use win32 api console functions to move the cursor to the desired line on the screen.

Edited 6 Years Ago by Ancient Dragon: n/a

I'm not really sure how to put it in it's own thread..... can you recommend a good resource that can help?

Try to use this if it helps...

#include <iostream>
#include <string>
#include <time.h>

using namespace std;
  
int main()
{
    char T[9];

    _strtime_s(T);

	cout<<T;
	
    return (0);
}

But your problem is still there... You can solve it by using an iteration algorithm using "goto" OR you can use timers, I have used timers in windows forms and they work very well, not sure if it is applicable to this problem.

Edited 6 Years Ago by Software guy: n/a

This article has been dead for over six months. Start a new discussion instead.