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)
Printable View
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.
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;
}
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? :pQuote:
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
>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.Quote:
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.
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! :pQuote:
Originally posted by Omnius
Catch up, Walt ;)
>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 "
*/