strftime() behaviour

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

}
 
T

Tor Rustad

(e-mail address removed) 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};
 
T

Tor Rustad

(e-mail address removed) 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);
 
D

David Thompson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top