Time is not updating

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
Have you tried putting in something to delay the program for at least a minute?

I have not. But wouldn't the seconds have updated?
Did a purposeful 2 minute delay, and had a similar result.
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
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.