c - sscanf to convert to a long integer -


can explain output of lines below:

sprintf(tempstr,"%s%2s%s",year_str,month_str,day_str);     count=sscanf(tempstr,"%ld%s",&tempout,other); 

it creates numeric date, using day, month , year values.

but, how convert numeric value long integer?

for e.g.: can tell me, if year, month , day 2016, 02, , 08, output value in tempout.

here, how input taken:

    char date_str[20];      char day_str[2];     char month_str[2];     char year_str[2];      time_t now;     struct tm* current_time;      /* current time */     = time(0);       /* convert time tm structure */     current_time = localtime(&now);      /* format day string */     sprintf(day_str,"%02d",current_time->tm_mday);      /* format month string */     sprintf(month_str,"%02d",current_time->tm_mon + 1);      /* format year string */     sprintf(year_str,"%d",current_time->tm_year);      /* assemble date string */     sprintf(date_str,"%s%2s%s",year_str,month_str,day_str); 

the output of when run (using http://cpp.sh/), is:

1160208

whereas thought should be:

20160208.

in other context, there line below, 116 if year 2016:

sprintf(year_str,"%02d",options.year - 1900);

here, date_str tempstr mentioned above, hence, input is: 1160208

the format specifier %ld implicitly typecasting numeric value long int, whereas, %d int alone.

edit:

i ran code now, using following data types of variables mentioned therein:

#include<stdio.h> #include<string.h> int main() {  char tempstr[100];  char year_str[5];  char month_str[3];  char day_str[3];  long int tempout;  char other[100];  int count;   strcpy(year_str, "2016");  strcpy(month_str ,"02");  strcpy(day_str ,"08");   sprintf(tempstr,"%s%2s%s",year_str,month_str,day_str);  count=sscanf(tempstr,"%ld%s",&tempout,other);   printf("tempstr: %s\ntempout: %ld\n", tempstr,tempout); } 

then did understand concern is, output showing:

tempstr: 20160208
tempout: 20160208

clearly, can see problem here in sprintf , not in sscanf.

the format specifier %s%2s%s have given sprintf means want concatenate 3 strings, year_str, month_str , day_str namely, without character ('/', space or '-') in between them. 2 in %2s means padding of 2 spaces possible.

this means when sscanf try read long int tempout read 20160208 1 number doing rightly meant do.

you therefore have add character such space - or / between year, month , day , work fine:

sprintf(tempstr,"%s %2s %s", year_str,month_str,day_str); 

new output is:

tempstr: 2016 02 08
tempout: 2016

edit2:

if @ man page of localtime() see ranges of members of struct tm are:

tm_mday

the day of month, in range 1 31.

tm_mon

the number of months since january, in range 0 11.

tm_year

the number of years since 1900.

which explains addition of 1 in sprintf(month_str,"%02d",current_time->tm_mon + 1); similarly, 1900 should added current_time->tm_year correct output:

sprintf(year_str,"%d",current_time->tm_year+1900); 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -