sscanf(datestr, "%[^-]-%[^-]-%[^;];",yr ,mont ,day );
returns for the datestring 2009-03-22
yr 200903
mont 03
day 22
what is wrong with the year?
Sat, 2014-11-29 20:44
#1
sscanf parseing a datestring
@JosDuchIt
Rewrite the format string (2nd parameter) as
"%[^'-']-%[^'-']-%[^';'];"
and check the return value
if ( sscanf() != 3 )
{
/* error */
}
assuming datestr, yr, mont, day declared and used correctly, not provided here.
@Massi,
thanks for the suggestion.
It does not work as expcted either though:
typical reasult of the code below
9.Stock:Dev/C_Snippets_Working> ram:parsedate
Please enter a datestring (separations with -).
1914-10-06;
You typed:
1914-10-06;
1914-10-06;
year 1914-10-06;
month -10-06;
day
year 1914-10-06;
month -10-06;
day
9.Stock:Backup_Datas/Gui4Cli/Dev/C_Snippets_Working> ram:parsedate
Please enter a datestring (separations with -).
1914-10-06;
You typed:
1914-10-06;
1914-10-06;
year 1914-10-06;
month -10-06;
day
year 1914-10-06;
month -10-06;
day
@JosDuchIt
You didn' t check the sscanf() return value, anyway.
Assuming your date format is YYYY-MM-DD;
1) yr should be declared as char yr[ 4 + 1 ], to be able to store the string termination character '\0' added by sscanf() itself (same for mont[ 2 + 1 ] and day[ 2 + 1 ])
2) if I remember well fgets() should read (BUFFERSIZE - 1) chars (see documentation)
3) change sscanf(datestr, " %[^'-']-%[^'-']-%[^';'];",&yr ,&mont ,&day ); with sscanf(datestr, " %[^'-']-%[^'-']-%[^';'];",yr ,mont ,day );
@Massi
Thanks
the yr[4] was the main culprit