sscanf parseing a datestring

5 posts / 0 new
Last post
JosDuchIt
JosDuchIt's picture
Offline
Last seen: 8 years 1 month ago
Joined: 2012-01-08 20:57
sscanf parseing a datestring

sscanf(datestr, "%[^-]-%[^-]-%[^;];",yr ,mont ,day );
returns for the datestring 2009-03-22
yr 200903
mont 03
day 22
what is wrong with the year?

Massi
Massi's picture
Offline
Last seen: 4 years 7 months ago
Joined: 2012-03-28 17:16
@JosDuchIt Rewrite the

@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.

JosDuchIt
JosDuchIt's picture
Offline
Last seen: 8 years 1 month ago
Joined: 2012-01-08 20:57
@Massi, thanks for the

@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

  1. #include <stdio.h> //puts , sscanf
  2. #include <string.h>//
  3.  
  4. #define BUFFERSIZE 30
  5. int main() {
  6.  
  7. char* bytes_read;
  8. char datestr[BUFFERSIZE];
  9. char yr[4], mont[9], day[9];
  10. char *year;
  11. char *month;
  12. char *thisday;
  13. puts ("Please enter a datestring (separations with -).");
  14. bytes_read = fgets (datestr, BUFFERSIZE, stdin);
  15. if (!(bytes_read))
  16. {
  17. puts ("ERROR!");
  18. }
  19. else
  20. {
  21. puts ("You typed:");
  22. puts (datestr);
  23. puts (bytes_read);
  24. //sscanf(datestr, " %[^-]-%[^-]-%[^;];",&yr ,&mont ,&day );
  25. sscanf(datestr, " %[^'-']-%[^'-']-%[^';'];",&yr ,&mont ,&day );
  26.  
  27. year = yr;
  28. month = mont;
  29. thisday = day;
  30. printf("year %s month %s day %s\n", yr, mont, day);
  31. printf("year %s month %s day %s\n", year, month, thisday);
  32.  
  33. }
  34. return 0;
  35. }
Massi
Massi's picture
Offline
Last seen: 4 years 7 months ago
Joined: 2012-03-28 17:16
@JosDuchIt You didn' t check

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

JosDuchIt
JosDuchIt's picture
Offline
Last seen: 8 years 1 month ago
Joined: 2012-01-08 20:57
@Massi Thanks the yr[4] was

@Massi

Thanks

the yr[4] was the main culprit

Log in or register to post comments