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)
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)
>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.*
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]
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
What's %li? Longitudinal Invariant?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
I'll bet you meant %ld
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
>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.*
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.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.
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
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!Originally posted by Omnius
Catch up, Walt
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
>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.*