I have come across very strange behavior of the return value of the
time() function. Below is the program to illustrate the problem: The
value of NOW somehow changes after the call to the ifstream
constructor.
#include <iostream>
#include <fstream>
#include <time.h>
int main(int argc, char** argv)
{
if (argc != 2)
{
std::cout << "Usage: ctest filename" << std::endl;
exit(-1);
}
time_t NOW = time(0);
std::cout << "Start time before the call to ifstream(): NOW="
<< NOW << std::endl;
std::ifstream infile(argv[1]);
std::cout << "Start time after the call to ifstream(): NOW="
<< NOW << std::endl;
infile.close();
exit(0);
}
I tried this program with g++ 3.1.1 compiler on Solaris 5.8 and here
is the output:
g++ -o test test.cxx
test test.cxx Time before the call to ifstream(): NOW=1083075439
Time after the call to ifstream(): NOW=27503
Given the signature of the time() (time_t time(time_t* t)) I was not
able to find any reasonable explanation how the call to the ifstream
constructor could affect the local variable it does not know about so
any help would be greatly appreciated.
By the way, I tried the same program with CC Sun Workshop 6.2 and it
works fine. CC -o test test.cxx
test test.cxx Time before the call to ifstream(): NOW=1083075848
Time after the call to ifstream(): NOW=1083075848
---
Thanks,
Mike Alexeev