H
HSeganfredo
Folks, my code below truncates the output as:
chars 0,200709
chars 0,1839
If I change to:
i = strftime(date, 9, "%Y%m%d", brokentime);
j = strftime(hour, 7, "%H%M%S", brokentime);
it gives:
chars 8,20070914
chars 6,184134
What is the output that I expected. So let me see if I am right:
strftime() does write an extra NUL char at the end of the formatted
char array, since it is a string, but does not report this NUL char as
the output (written chars) of the strftime() function? Funny
behaviour...
-------------------------------------
#include <stdio.h>
#include <string.h>
int main(){
char *date; initstring(&date, '\0', 8); /* initialized to 8 '\0' chars
+ 1 '\0' */
char *hour; initstring(&hour, '\0', 6); /* initialized to 6 '\0' chars
+ 1 '\0' */
/* necessita padding na date/hour? (20070101?) */
struct tm *brokentime;
time_t rawtime;
rawtime = time(NULL);
brokentime = gmtime(&rawtime);
int i=0; int j=0;
i = strftime(date, 8, "%Y%m%d", brokentime);
j = strftime(hour, 6, "%H%M%S", brokentime);
printf("\nchars %d,", i); puts(date);
printf("\nchars %d,", j); puts(hour);
return(0);
}
chars 0,200709
chars 0,1839
If I change to:
i = strftime(date, 9, "%Y%m%d", brokentime);
j = strftime(hour, 7, "%H%M%S", brokentime);
it gives:
chars 8,20070914
chars 6,184134
What is the output that I expected. So let me see if I am right:
strftime() does write an extra NUL char at the end of the formatted
char array, since it is a string, but does not report this NUL char as
the output (written chars) of the strftime() function? Funny
behaviour...
-------------------------------------
#include <stdio.h>
#include <string.h>
int main(){
char *date; initstring(&date, '\0', 8); /* initialized to 8 '\0' chars
+ 1 '\0' */
char *hour; initstring(&hour, '\0', 6); /* initialized to 6 '\0' chars
+ 1 '\0' */
/* necessita padding na date/hour? (20070101?) */
struct tm *brokentime;
time_t rawtime;
rawtime = time(NULL);
brokentime = gmtime(&rawtime);
int i=0; int j=0;
i = strftime(date, 8, "%Y%m%d", brokentime);
j = strftime(hour, 6, "%H%M%S", brokentime);
printf("\nchars %d,", i); puts(date);
printf("\nchars %d,", j); puts(hour);
return(0);
}