sscanf question

Discussion in 'C Programming' started by vw_bora@sky.com, Jul 1, 2009.

  1. Guest

    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
     
    , Jul 1, 2009
    #1
    1. Advertising

  2. On Jul 2, 7:42 am, wrote:
    > 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.

    --
    Peter
     
    Peter Nilsson, Jul 1, 2009
    #2
    1. Advertising

  3. Guest

    On 1 July, 23:18, Peter Nilsson <> wrote:
    > On Jul 2, 7:42 am, wrote:
    >
    > > 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.
    >
    > --
    > Peter


    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
     
    , Jul 2, 2009
    #3
  4. Guest

    On 2 July, 03:46, Kenneth Brody <> wrote:
    > 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 Brody



    Kenneth

    Top suggestion ... done the trick.


    Thank you.

    Pete
     
    , Jul 2, 2009
    #4
  5. Moi Guest

    On Wed, 01 Jul 2009 18:37:54 -0700, vw_bora wrote:

    > On 1 July, 23:18, Peter Nilsson <> wrote:
    >> On Jul 2, 7:42 am, wrote:
    >>
    >> > 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.
    >>
    >> --
    >> Peter

    >
    > 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
     
    Moi, Jul 2, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan Smith

    Question on sscanf

    Dan Smith, Aug 14, 2003, in forum: C Programming
    Replies:
    3
    Views:
    427
    Martin Ambuhl
    Aug 14, 2003
  2. Brent Lievers

    sscanf fixed-width integer question

    Brent Lievers, Apr 22, 2004, in forum: C Programming
    Replies:
    8
    Views:
    665
    Dan Pop
    Apr 26, 2004
  3. sscanf() question?

    , Jun 9, 2005, in forum: C Programming
    Replies:
    5
    Views:
    539
  4. broeisi

    Programmer wannabee question about sscanf

    broeisi, Mar 4, 2006, in forum: C Programming
    Replies:
    10
    Views:
    545
    CBFalconer
    Mar 5, 2006
  5. sscanf question

    , Nov 22, 2006, in forum: C Programming
    Replies:
    5
    Views:
    645
Loading...

Share This Page