Click to See Complete Forum and Search --> : sscanf problem


frei
March 11th, 2007, 04:44 PM
Hello

I'm parsing a file with sscanf for strings in the following two formats:
2006 03 Mar ../test/usage_200603.html 755 500 310 28 604 289.19 MB 897 9623 15502 23415
2006 01 Jan ../test/usage_200601.html 10647 6524 4192 113 2146 1263731 3520 129956 202261 330065

The problem is that the code below works for a string of the second format only. But if I want to parse a string of the first format the code doesn't work anymore. Reason: In this case the 10th entry is a string but NOT an integer as in second case. Result: The string will not be parsed (13 entries only)...

Does someone have a code solution for parsing both formats with sscanf in one code routine?? Would be great!! Thank you!!

if (sscanf(inbuf,
"%4d %2d %3s %s "
"%llu %llu %llu %llu %llu "
"%llu %llu %llu %llu %llu\n",
&summary_year, &summary_month, summary_mname,
summary_path, &summary_dhits, &summary_dfiles,
&summary_dpages, &summary_dvisits, &summary_tsites,
&summary_tkbytes, &summary_tvisits, &summary_tpages,
&summary_tfiles, &summary_thits) == 14)

Paul McKenzie
March 11th, 2007, 08:18 PM
Honestly, scanf() is not meant to be a parser. If you want to parse the data, read the entire line in as a string and parse the string yourself.

Your parsing logic will beat scanf() all the time, since you know how and where the fields are, and scanf() is just a generic input function without any knowledge of any wrinkle your input may have.

I practically cringe when I see persons try to use scanf() as a poor man's parser. It almost never works out, and you leave yourself wide open for any idiosyncracies scanf() may have that is unknown to you. Again, better to write your own parser.

Regards,

Paul McKenzie