Discussion:
fscanf formatting oddity & question
(too old to reply)
R.Wieser
2022-06-16 14:54:28 UTC
Permalink
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
R.Wieser
2022-06-16 15:22:45 UTC
Permalink
Hmmm ...
Post by R.Wieser
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
Paul N
2022-06-18 19:40:14 UTC
Permalink
Post by R.Wieser
Hello all,
" someid=%u%[;]c"
Only if the ";" is encountered the result of fscanf equals 2 (can't use
"%n", as that doesn't up the result count).
(snip)
Post by R.Wieser
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.
R.Wieser
2022-06-19 08:15:30 UTC
Permalink
Paul,
Post by Paul N
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.
Post by Paul N
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

Loading...