time_t

This is a discussion on time_t within the C Programming forums, part of the General Programming Boards category; how do you do to save the calendar time in a file? one of the parameters in fprintf is the ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    17

    time_t

    how do you do to save the calendar time in a file?
    one of the parameters in fprintf is the format of the variable I want to put in a file but what do I put?

    Code:
    time_t t;
    FILE* p;
    fprintf(p, "%? ", t)

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >how do you do to save the calendar time in a file?
    >one of the parameters in fprintf is the format of the variable I want to put in a file but what do I put?
    • You could look in time.h for your implementation's definition of time_t, then use an appropriate specifier.
    • You could cast the time_t value to some type and use the format specifier for that type.
    • You could use gmtime or localtime to convert the time_t to broken-down time and then fprintf its component values.
    • You could use strftime to convert the broken-down time to a string and then fprintf the string.
    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.*

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Some examples in the FAQ

    Maybe this is what you want:
    Code:
    #include <stdio.h> 
    #include <time.h> 
    
    int main ( void )
    {
      time_t now;
      now = time ( NULL );
      printf ( "%s", ctime ( &now ) );
      return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    don't want you to use the cheap way out, but time_t on my system is defined as a long int. %li
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by chrismiceli
    don't want you to use the cheap way out, but time_t on my system is defined as a long int. %li
    What's %li? Longitudinal Invariant?

    I'll bet you meant %ld
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >What's %li?

    For the printf family, "%li" behaves the same as "%ld". They have different behaviors for the scanf family.
    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.*

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by Dave_Sinkula
    >What's %li?

    For the printf family, "%li" behaves the same as "%ld". They have different behaviors for the scanf family.
    Really?! In 20 years of programming in C, I don't think I've ever used a %i, and almost never a scanf(). Learn something new every hour. Thanks Dave.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Catch up, Walt

  9. #9
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by Omnius
    Catch up, Walt
    I read the specs on %i -- I don't need two different ways to output an int, %d is identical. As for scanf(), as I said I don't use it, I have no need for %i. Although I have to at least try it, just so I can figure out what "... with the value 0 for the base argument" means. Base 2 I understand. Base 1 sounds useless. Base 0? Sounds like a black hole!
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >As for scanf(), as I said I don't use it, I have no need for %i. Although I have to at least try it, just so I can figure out what "... with the value 0 for the base argument" means.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       const char *text[] = {"   42 ","  042 "," 0x42 ", " FACE "};
       size_t i;
       for ( i = 0; i < sizeof text / sizeof *text; ++i )
       {
          int value;
          printf("text[%lu] = \"%s\"", (long unsigned)i, text[i]);
          if ( sscanf(text[i], "%d", &value) == 1 )
          {
             printf(", using \"%%d\" value = %2d", value);
          }
          if ( sscanf(text[i], "%i", &value) == 1 )
          {
             printf(", using \"%%i\" value = %2i", value);
          }
          putchar('\n');
       }
       return 0;
    }
    
    /* my output
    text[0] = "   42 ", using "%d" value = 42, using "%i" value = 42
    text[1] = "  042 ", using "%d" value = 42, using "%i" value = 34
    text[2] = " 0x42 ", using "%d" value =  0, using "%i" value = 66
    text[3] = " FACE "
    */
    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