469,610 Members | 1,854 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

strftime() behaviour

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);

}

Sep 14 '07 #1
3 2017
HS*********@gmail.com wrote:

[...]
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' */
You need to allocate space for 'date' and 'hour', e.g. like this:

char date[8] = {0};
char hour[6] = {0};
--
Tor <torust [at] online [dot] no>
Sep 14 '07 #2
HS*********@gmail.com wrote:

[...]
i = strftime(date, 8, "%Y%m%d", brokentime);
j = strftime(hour, 6, "%H%M%S", brokentime);
Hmmm...

IIRC, you need 4 for year, 2 for month, 2 for day and 1 for '\0', that's
4+2+2+1 = 9.

char date[9];

strftime(date, sizeof date, "%Y%m%d", brokentime);
--
Tor <torust [at] online [dot] no>
Sep 14 '07 #3
On Fri, 14 Sep 2007 11:45:16 -0700, HS*********@gmail.com wrote:
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...
Exactly. The \0 is needed to ensure it's a valid string, but is not
considered part of the string contents. [v]snprintf () does the same.

fgets() stores a \0 within the given limit; it does not return a
count, but a subsequent strlen() on the buffer will return the count
EXcluding the \0. Well, unless the data read from the file includes a
null byte or bytes; but fgets() is usually (though not necessarily)
used on text and particularly text streams, which aren't portably
required to support containing null bytes, and IMO shouldn't.

If you want to malloc space to store (e.g. copy) a string, you must
use strlen(s)+1. (Or more, if that is (more) convenient.)

OTOH *scanf %Ns or %N[...] stores up to N bytes PLUS a \0.
Nothing in this world is purrfect. <G>

- formerly david.thompson1 || achar(64) || worldnet.att.net
Sep 23 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Richard Shea | last post: by
4 posts views Thread by John Hunter | last post: by
4 posts views Thread by Andy Leszczynski | last post: by
1 post views Thread by Dennis Benzinger | last post: by
2 posts views Thread by Giovanni Bajo | last post: by
3 posts views Thread by Andrii V. Mishkovskyi | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.