time_t

This is a discussion on time_t within the C Programming forums, part of the General Programming Boards category; Okay, I can figure out how to print out a string with the time information, but what about storing it ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    >> 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:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >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 11:16 AM.
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21