CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: sscanf problem

  1. #1
    Join Date
    Mar 2000
    Posts
    196

    sscanf problem

    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!!

    Code:
    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)

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: sscanf problem

    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
    Last edited by Paul McKenzie; March 12th, 2007 at 12:20 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured