Search code examples
c++ctimestat

C++ convert string to time_t


I'm using stat.st_mtime to get my last modification time of a directory and then store it into a file.txt (stored string is something like that : 1467035651)

Later when I retrieved data from my file.txt, I tried to convert my string from my file.txt to int type since the string contains just seconds but I don't know if it's a good idea to do that.

Is there any way to convert directly to time_t?


Solution

  • According to the reference documentation time_t is just an unspecified numeric format.

    Just directly read back the numeric value from the file, there's no need for special conversions:

    time_t t;
    std::ifstream input("File.txt");
    // ...
    input >> t;