Time is not updating
Jan 3, 2013 at 7:37pm UTC
I have code that references the function then adds "Process Started", does about 30 seconds worth of processing (in the test) and then puts in a separate line "Process Completed". The time never updates. To my knowledge, everything is formatted properly outside of this function. Here is a snipet of code along with the function that adds the time:
1 2 3 4 5
TDLogFile();
LogFile << "Process started." << endl;
(30 seconds worth of stuff to do )
TDLogFile();
LogFile << "Process Completed." << endl;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void TDLogFile(){
ofstream LogFile;
time_t now = time(0);
tm *ltm = localtime(&now);
LogFile.open ("logfile.log" , ios::out | ios::app);
time_t rawtime;
struct tm * timeinfo;
char buffer [80];
time ( &rawtime );
timeinfo = localtime ( &rawtime );
strftime (buffer,80,"%Y-%m-%d %X " ,timeinfo);
LogFile << buffer;
LogFile.close();
}
The output is as follows:
2013-01-03 09:26:33 Process Started.
2013-01-03 09:26:33 Process Completed.
Any help is appreciated and will provide much self-satisfaction. Which is really its own reward.
Last edited on Jan 3, 2013 at 9:59pm UTC
Jan 3, 2013 at 8:11pm UTC
Have you tried putting in something to delay the program for at least a minute?
Jan 3, 2013 at 9:32pm UTC
I have not. But wouldn't the seconds have updated?
Jan 3, 2013 at 10:04pm UTC
Did a purposeful 2 minute delay, and had a similar result.
Jan 3, 2013 at 10:11pm UTC
I has a dumb just now. I botched the order things were done in.
1 2 3 4 5
TDLogFile();
LogFile << "Process started." << endl;
TDLogFile();
LogFile << "Process Completed." << endl;
(30 seconds worth of stuff to do )
It's fixed now. /facepalm
Jan 3, 2013 at 10:20pm UTC
I called you function like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#include <ctime>
#include <iostream>
#include <fstream>
void TDLogFile()
{
std::ofstream LogFile;
time_t now = time(0);
tm *ltm = localtime(&now);
LogFile.open ("logfile.log" , std::ios::app);
time_t rawtime;
struct tm * timeinfo;
char buffer [80];
time ( &rawtime );
timeinfo = localtime ( &rawtime );
strftime (buffer,80,"%Y-%m-%d %X \n" ,timeinfo);
LogFile << buffer;
LogFile.close();
}
int main()
{
TDLogFile();
std::cin.get();
TDLogFile();
}
}
Notice that I added the new line character so my output looked like:
1 2
2013-01-03 16:17:46
2013-01-03 16:17:48
As you can see it works for me. Also if you are using this for a log file you may want to see about using an unbuffered file stream.
Topic archived. No new replies allowed.