Thread: time_t

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    Okay, I can figure out how to print out a string with the time information, but what about storing it in a binary file specifically for the purpose of pulling it out to do calculations with?

    Do I just do a fwrite with the tm pointer or something like that?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Split from old post
    Sure you can fwrite and fread it, but its a very non-portable way of doing it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Do I just do a fwrite with the tm pointer or something like that?

    Well, fwrite()'ing pointer to *anything* won't ever work.

    time_t is just a typedef for a certain size of int (the size being dependant on the system).

    A portable way to do it would be:

    time_t stamp;
    //...stuff
    fwrite(&stamp, 1, sizeof(time_t), out);
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >time_t is just a typedef for a certain size of int

    No. A time_t is an arithmetic type, which includes integer and floating types.

    The method you've shown isn't portable between implementations that represent time_t differently.
    [edit]
    By that I mean that if the file was written with representation A but read with representation B, and vice-versa, the results might be unexpected. This could be so even from the same source code built on different implementations.
    [/edit]
    Last edited by Dave_Sinkula; 03-10-2004 at 12:16 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed