• fscanf formatting oddity & question

    From R.Wieser@21:1/5 to All on Thu Jun 16 16:54:28 2022
    Hello all,

    I'm trying to use fscanf to read a certain string, using this formatting :

    " someid=%u%[;]c"

    The latter part, "%[;]c" is a trick to detect if the whole string is read : Only if the ";" is encountered the result of fscanf equals 2 (can't use
    "%n", as that doesn't up the result count).

    The thing is that I actually took a look at what got stored, and found that instead of a single byte, 0x3B, *two* where stored, 0x003B. When I remove
    the "[;]" it goes back to storing a just single byte.

    Can somebody explain to my why the extra 0x00 happens (what the reason for
    it is) ?

    Second question : Is there some way I can tell fscanf that it should match
    upto the terminating zero (so I can do away with the "%[;]c" cludge
    altogether) ?

    Regards,
    Rudy Wieser

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From R.Wieser@21:1/5 to All on Thu Jun 16 17:22:45 2022
    Hmmm ...

    The latter part, "%[;]c" is a trick to detect if the whole string is read

    Hmmm ... It seems I misunderstood what the "[","]" brackets are for, and
    from it I see that in the above the "c" isn't regarded as a formatting specifier. It also explains the 'extra' "0x00" in the result.

    And as a result the above needs to be changed to "%1[;]".


    The second part of my question still stands though, as I would gladly do
    away with the the whole trick (of sorts).

    .. remarkable how answers always seem to pop up /after/ the question about
    them is submitted. :-)

    Regards,
    Rudy Wieser

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul N@21:1/5 to R.Wieser on Sat Jun 18 12:40:14 2022
    On Thursday, June 16, 2022 at 3:54:41 PM UTC+1, R.Wieser wrote:
    Hello all,

    I'm trying to use fscanf to read a certain string, using this formatting :

    " someid=%u%[;]c"

    The latter part, "%[;]c" is a trick to detect if the whole string is read : Only if the ";" is encountered the result of fscanf equals 2 (can't use "%n", as that doesn't up the result count).

    (snip)

    Second question : Is there some way I can tell fscanf that it should match upto the terminating zero (so I can do away with the "%[;]c" cludge altogether) ?

    If you're using fscanf there presumably isn't a terminating zero, you will be looking at '\n' or EOF or the like. I think it is recommended not to use fscanf, rather to read in a line and process it using sscanf, which gives more control. Of course
    whether this is worthwhile will depend on the details, eg whether you want to simply ignore and/or complain about badly formed lines, or whether you want to abort the whole reading.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From R.Wieser@21:1/5 to All on Sun Jun 19 10:15:30 2022
    Paul,

    If you're using fscanf there presumably isn't a terminating zero,

    The terminating zero was ment in regard to the formatting string.

    The problem is that (f|s)scanf will simply ignore any part it cannot match
    to the provided format string. And in my case that just doesn't work.

    Of course whether this is worthwhile will depend on the details, eg
    whether you
    want to simply ignore and/or complain about badly formed lines, or whether you
    want to abort the whole reading.

    I have not considered trying to find the end of a badly-formatted (part of
    a) line so I can continue reading the next string. So, aborting - or
    rather terminating the scanning for more strings - on a "badly-formatted"
    line it is.

    I'm already considering a (better) way to check if I have an unrecognised formatting string, or if the target is actually badly formatted.

    It doesn't change anything to the question though - can I tell (f|s)scanf to force a full matching of the formatting string (upto its terminating zero) ? Other than the kludges I've mentioned I mean.

    Regards,
    Rudy Wieser

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)