Warning in data format of the fscanf function

Discussion in 'C Programming' started by InuY4sha, Aug 7, 2007.

  1. InuY4sha

    InuY4sha Guest

    Hi,
    I hope to be not off topic..
    I have a string on a file "00:32:43:54:A2:2D" (let's say a MAC
    address)
    I use fscanf like this
    fscanf( file, "%02X:%02X:%02X:%02X:%02X:%02X",
    ptr, ptr+1, ptr+2,
    ptr3, ptr+4, ptr+5);
    It works, but I get warnings as the expected size is int while I'm
    storing onto a uint8_t array.. is there a format to store onto a
    uint8_t array instead of using a int array? ( I mean to don't get any
    warning)
    Things are working but I hate that warnings..
    Thanks
    InuY4sha, Aug 7, 2007
    #1
    1. Advertising

  2. InuY4sha

    Army1987 Guest

    On Tue, 07 Aug 2007 10:11:38 +0000, InuY4sha wrote:

    > Hi,
    > I hope to be not off topic..
    > I have a string on a file "00:32:43:54:A2:2D" (let's say a MAC
    > address)
    > I use fscanf like this
    > fscanf( file, "%02X:%02X:%02X:%02X:%02X:%02X",
    > ptr, ptr+1, ptr+2,
    > ptr3, ptr+4, ptr+5);
    > It works, but I get warnings as the expected size is int while I'm
    > storing onto a uint8_t array.. is there a format to store onto a
    > uint8_t array instead of using a int array? ( I mean to don't get any
    > warning)
    > Things are working but I hate that warnings..

    They are working because you're lucky enough. "Something" tells me
    that your machine is little endian. (Figuring out what this
    "something" is is left as an exercise.)

    Use the macro SCNx8 in <inttypes.h> which expands to a string
    literal, like this:
    fscanf(file, "%02" SCNx8 ":%02" SCNx8 ":%02" SCNx8 ":%02" SCNx8
    ":%02" SCNx8 ":%02" SCNx8, ptr, ptr+1, ptr+2, ptr+3,
    /* you didn't actually mean ptr3, did you? */, ptr+4, ptr+5);

    This work due to string concatenation. For example, if SCNx8
    expands to "hhx", "%02" SCNx8 ":%02" expands to "%02" "hhx" ":%02"
    which is equivalent to "%02hhx:%02".
    --
    Army1987 (Replace "NOSPAM" with "email")
    "Never attribute to malice that which can be adequately explained
    by stupidity." -- R. J. Hanlon (?)
    Army1987, Aug 7, 2007
    #2
    1. Advertising

  3. InuY4sha

    InuY4sha Guest

    On 7 Ago, 12:46, Army1987 <> wrote:
    > On Tue, 07 Aug 2007 10:11:38 +0000, InuY4sha wrote:
    > > Hi,
    > > I hope to be not off topic..
    > > I have a string on a file "00:32:43:54:A2:2D" (let's say a MAC
    > > address)
    > > I use fscanf like this
    > > fscanf( file, "%02X:%02X:%02X:%02X:%02X:%02X",
    > > ptr, ptr+1, ptr+2,
    > > ptr3, ptr+4, ptr+5);
    > > It works, but I get warnings as the expected size is int while I'm
    > > storing onto a uint8_t array.. is there a format to store onto a
    > > uint8_t array instead of using a int array? ( I mean to don't get any
    > > warning)
    > > Things are working but I hate that warnings..

    >
    > They are working because you're lucky enough. "Something" tells me
    > that your machine is little endian. (Figuring out what this
    > "something" is is left as an exercise.)
    >
    > Use the macro SCNx8 in <inttypes.h> which expands to a string
    > literal, like this:
    > fscanf(file, "%02" SCNx8 ":%02" SCNx8 ":%02" SCNx8 ":%02" SCNx8
    > ":%02" SCNx8 ":%02" SCNx8, ptr, ptr+1, ptr+2, ptr+3,
    > /* you didn't actually mean ptr3, did you? */, ptr+4, ptr+5);
    >
    > This work due to string concatenation. For example, if SCNx8
    > expands to "hhx", "%02" SCNx8 ":%02" expands to "%02" "hhx" ":%02"
    > which is equivalent to "%02hhx:%02".
    > --
    > Army1987 (Replace "NOSPAM" with "email")
    > "Never attribute to malice that which can be adequately explained
    > by stupidity." -- R. J. Hanlon (?)


    This way I don't get the warning but it doesn't seem to be working
    fine as the data retrieved is wrong... (while before it was fine..)
    >From what I see the printed data is totally random.
    InuY4sha, Aug 7, 2007
    #3
  4. InuY4sha

    InuY4sha Guest


    > This way I don't get the warning but it doesn't seem to be working
    > fine as the data retrieved is wrong... (while before it was fine..)
    >
    > >From what I see the printed data is totally random.


    Sorry it was my mistake... I did as you said and it worked.. as it
    worked with %02hhX... why shouldn't I use this last solution instead..
    as it-s reported in the man page of fscanf ?
    InuY4sha, Aug 7, 2007
    #4
  5. InuY4sha

    Army1987 Guest

    On Tue, 07 Aug 2007 12:03:56 +0000, InuY4sha wrote:

    >
    >> This way I don't get the warning but it doesn't seem to be working
    >> fine as the data retrieved is wrong... (while before it was fine..)
    >>
    >> >From what I see the printed data is totally random.

    >
    > Sorry it was my mistake... I did as you said and it worked.. as it
    > worked with %02hhX... why shouldn't I use this last solution instead..
    > as it-s reported in the man page of fscanf ?

    It works because uint8_t is unsigned char, and hhX is a conversion
    specifier for unsigned char. (I think it is very unlikely that
    there is a system where uint8_t exists but it is not unsigned
    char, though I think the standard does allow it to be an extended
    type with the same size and range, even if I can't see any
    usefulness in ever doing that.)
    --
    Army1987 (Replace "NOSPAM" with "email")
    "Never attribute to malice that which can be adequately explained
    by stupidity." -- R. J. Hanlon (?)
    Army1987, Aug 7, 2007
    #5
  6. InuY4sha <> writes:

    > On 7 Ago, 12:46, Army1987 <> wrote:
    >> On Tue, 07 Aug 2007 10:11:38 +0000, InuY4sha wrote:
    >> > Hi,
    >> > I hope to be not off topic..
    >> > I have a string on a file "00:32:43:54:A2:2D" (let's say a MAC
    >> > address)
    >> > I use fscanf like this
    >> > fscanf( file, "%02X:%02X:%02X:%02X:%02X:%02X",
    >> > ptr, ptr+1, ptr+2,
    >> > ptr3, ptr+4, ptr+5);
    >> > It works, but I get warnings as the expected size is int while I'm
    >> > storing onto a uint8_t array.. is there a format to store onto a
    >> > uint8_t array instead of using a int array? ( I mean to don't get any
    >> > warning)
    >> > Things are working but I hate that warnings..

    <snip>
    >> Use the macro SCNx8 in <inttypes.h> which expands to a string
    >> literal, like this:
    >> fscanf(file, "%02" SCNx8 ":%02" SCNx8 ":%02" SCNx8 ":%02" SCNx8
    >> ":%02" SCNx8 ":%02" SCNx8, ptr, ptr+1, ptr+2, ptr+3,
    >> /* you didn't actually mean ptr3, did you? */, ptr+4, ptr+5);

    <snip>
    > This way I don't get the warning but it doesn't seem to be working
    > fine as the data retrieved is wrong... (while before it was fine..)
    > From what I see the printed data is totally random.


    I think you'll have to post a short example of this "not working".
    Ideally a full program that read the data from stdin. Often, just
    trying to generate a short example of the problem, you will find out
    what is wrong for yourself.

    --
    Ben.
    Ben Bacarisse, Aug 7, 2007
    #6
  7. InuY4sha

    Richard Bos Guest

    InuY4sha <> wrote:

    > I hope to be not off topic..


    I'm writing this reply presuming you aren't.

    > I have a string on a file "00:32:43:54:A2:2D" (let's say a MAC
    > address)
    > I use fscanf like this
    > fscanf( file, "%02X:%02X:%02X:%02X:%02X:%02X",
    > ptr, ptr+1, ptr+2,
    > ptr3, ptr+4, ptr+5);
    > It works, but I get warnings as the expected size is int while I'm
    > storing onto a uint8_t array.. is there a format to store onto a
    > uint8_t array instead of using a int array? ( I mean to don't get any
    > warning)


    If you're using uint8_t, you're using C99. If you're using C99, you
    should also have <inttypes.h>. If you #include <inttypes.h> (at which
    point you can ditch <stdint.h>, since that is automatically included by
    <inttypes.h>), you can use SCNx8. For example, instead of

    fscanf(file, "%02X...", ptr);

    you can use

    fscanf(file, "%02" SCNx8 "...", ptr);

    and so on.
    <inttypes.h> also defines similar macros for other sized integers, for
    int_fast<foo>, int_least<bar>, intmax_t, and so on; and for the printf()
    family as well as the scanf()s. Less relevantly, there are also a couple
    of useful functions for dealing with (u)intmax_t.

    Richard
    Richard Bos, Aug 7, 2007
    #7
    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. Replies:
    0
    Views:
    314
  2. John
    Replies:
    4
    Views:
    613
    Keith Thompson
    Sep 27, 2006
  3. Replies:
    7
    Views:
    460
    Default User
    Sep 5, 2006
  4. David Mathog
    Replies:
    59
    Views:
    1,405
    Kelsey Bjarnason
    Sep 3, 2007
  5. Disc Magnet
    Replies:
    3
    Views:
    717
    David Thompson
    Mar 24, 2010
Loading...

Share This Page