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

Recommended Answers

All 5 Replies

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.

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

what operating system? MS-Windows use win32 api function CreateThread(). See these links for examples

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.

timers -- e.g. SetTime() and KillTimer() -- do not work with console programs because they do not have message pumps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.