sscanf question

V

vw_bora

Hi

I am trying to use sscanf to to read " name value" pairs with the
value being either an unsigned 8bit or 16 bit or 32bit or 64bit
numberic value.

name1 255
name2 65535
name3 4294967295
name4 42949672954294967295

Can you please suggest how I can use sscanf to achieve this please.

Thank you

Pete
 
P

Peter Nilsson

I am trying to use sscanf to to read " name value" pairs
with the value being either an unsigned 8bit or 16 bit or
32bit or 64bit numberic value.

Well a 32bit value will fit in a 64bit value, as will a 16bit
value and an 8bit value.
name1     255
name2    65535
name3    4294967295
name4    42949672954294967295

Can you please suggest how I can use sscanf to achieve this
please.

Scan for name with %s or %[, scan for an unsigned long long
with %llu.
 
V

vw_bora

I am trying to use sscanf to to read " name value" pairs
with the value being either an unsigned 8bit or 16 bit or
32bit or 64bit numberic value.

Well a 32bit value will fit in a 64bit value, as will a 16bit
value and an 8bit value.
name1     255
name2    65535
name3    4294967295
name4    42949672954294967295
Can you please suggest how I can use sscanf to achieve this
please.

Scan for name with %s or %[, scan for an unsigned long long
with %llu.

Hi Peter
Thanks for the reply.
I should have said I am using Visual C++ 6.0.
Taking your suggestion I am able to read the values as required
( shown below ) except for the 64 bit value.


Reading the 8 bit value is OK
unsigned char val8bit
sscan ( buffer"%s %u, name, &val8bit)

Reading the 8 bit value is OK
unsigned char val8bit
sscan ( buffer"%s %u, name, &val8bit)

Reading the 16 bit value is OK
unsigned short int val16bit
sscan ( buffer"%s %d, name, &val16bit)

Reading the 32 bit value is OK
unsigned long int val32bit
sscan ( buffer"%s %lu, name, &val16bit)

Reading the 64 bit value is NOT OK
unsigned long long val64bit
sscan ( buffer"%s %llu, name, &val64bit)


I also noticed that if the value was greater than the variable storage
size, this would result in memory corrption. ie

buffer "name 65535"
unsigned char val8bit
sscan ( buffer"%s %u, name, &val8bit)

would result in writing 2 bytes starting at &val8bit when only 1 byte
was allocated. Is there a way to read values o ensure they are in the
valid range a according to the type.

Many Thanks

Pete
 
V

vw_bora

(e-mail address removed) wrote:

[... MS Visual C 6 ...]> Reading the 64 bit value is NOT OK
   unsigned long long val64bit
   sscan ( buffer"%s %llu, name, &val64bit)

[...]

According to <http://msdn.microsoft.com/en-us/library/xdb9w69d(VS.71).aspx>
you need to use a type "__int64" and the scanf format specifier "%I64u".
(It may be that "__int64" is a typedef for "long long".  I don't know.)


Kenneth

Top suggestion ... done the trick.


Thank you.

Pete
 
M

Moi

I am trying to use sscanf to to read " name value" pairs with the
value being either an unsigned 8bit or 16 bit or 32bit or 64bit
numberic value.

Well a 32bit value will fit in a 64bit value, as will a 16bit value and
an 8bit value.
name1     255
name2    65535
name3    4294967295
name4    42949672954294967295
Can you please suggest how I can use sscanf to achieve this please.

Scan for name with %s or %[, scan for an unsigned long long with %llu.

Hi Peter
Thanks for the reply.
I should have said I am using Visual C++ 6.0. Taking your suggestion I
am able to read the values as required ( shown below ) except for the 64
bit value.


Reading the 8 bit value is OK
unsigned char val8bit
sscan ( buffer"%s %u, name, &val8bit)

Wrong. %u expects an unsigned int. val8bit is only 8 bits wide.
Reading the 8 bit value is OK
unsigned char val8bit
sscan ( buffer"%s %u, name, &val8bit)

Idem
Reading the 16 bit value is OK
unsigned short int val16bit
sscan ( buffer"%s %d, name, &val16bit)

Idem. (in this case %hu could do the trick)

Reading the 32 bit value is OK
unsigned long int val32bit
sscan ( buffer"%s %lu, name, &val16bit)

Typo ?
Reading the 64 bit value is NOT OK
unsigned long long val64bit
sscan ( buffer"%s %llu, name, &val64bit)


This one should work.

(except for scanning a character into a limited size array , using %s is
dangerous; but that's another issue. )
I also noticed that if the value was greater than the variable storage
size, this would result in memory corrption. ie

buffer "name 65535"
unsigned char val8bit
sscan ( buffer"%s %u, name, &val8bit)

would result in writing 2 bytes starting at &val8bit when only 1 byte
was allocated. Is there a way to read values o ensure they are in the
valid range a according to the type.


Exactly. sscanf()s man page says something like:
%u expects (a pointer to) an unsigned int
, so you'd better give it one.

HTH,
AvK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top