sscanf

Discussion in 'C Programming' started by Allan Bruce, Apr 16, 2004.

  1. Allan Bruce

    Allan Bruce Guest

    If I have
    sscanf("FL:%s:%d:%s\n", lGuid, &lID, lFileName);

    and the last string contains spaces, e.g. my complete string
    "FL:1234ABCD:3:FileName With Spaces.txt\n"

    does sscanf just make lFileName the string up to the whitespace? even though
    I tell it the end of string as at the \n ?

    Thanks
    Allan
     
    Allan Bruce, Apr 16, 2004
    #1
    1. Advertising

  2. Allan Bruce

    Allan Bruce Guest

    "Allan Bruce" <> wrote in message
    news:c5o8ge$a8j$2surf.net...
    > If I have
    > sscanf("FL:%s:%d:%s\n", lGuid, &lID, lFileName);
    >
    > and the last string contains spaces, e.g. my complete string
    > "FL:1234ABCD:3:FileName With Spaces.txt\n"
    >
    > does sscanf just make lFileName the string up to the whitespace? even

    though
    > I tell it the end of string as at the \n ?
    >
    > Thanks
    > Allan
    >
    >


    also, is there a way to set ':' as a terminator for the string? I jsut
    realised that my lGuid will continue until the next whitespace or \0
    Thanks
    Allan
     
    Allan Bruce, Apr 16, 2004
    #2
    1. Advertising

  3. "Allan Bruce" <> wrote:
    >
    >"Allan Bruce" <> wrote:
    >> If I have
    >> sscanf("FL:%s:%d:%s\n", lGuid, &lID, lFileName);

    ^
    You forgot to specify the string to parse

    >> and the last string contains spaces, e.g. my complete string
    >> "FL:1234ABCD:3:FileName With Spaces.txt\n"
    >>
    >> does sscanf just make lFileName the string up to the whitespace? even

    >though
    >> I tell it the end of string as at the \n ?

    >
    >also, is there a way to set ':' as a terminator for the string? I jsut
    >realised that my lGuid will continue until the next whitespace or \0


    Warning, untested:

    sscanf( yourstring, "FL:%[^:]:%d:%[^\n]\n", lGuid, &lID, lFileName);

    If the ':'s might be surrounded by whitespace that should be ignored,
    you'll have to match and discard that, too.

    HTH
    Regards
    --
    Irrwahn Grausewitz ()
    welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
    clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
    clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
     
    Irrwahn Grausewitz, Apr 16, 2004
    #3
  4. Allan Bruce

    Allan Bruce Guest

    >
    > Warning, untested:
    >
    > sscanf( yourstring, "FL:%[^:]:%d:%[^\n]\n", lGuid, &lID, lFileName);
    >
    > If the ':'s might be surrounded by whitespace that should be ignored,
    > you'll have to match and discard that, too.
    >
    > HTH
    > Regards


    I tried:
    sscanf(xiBuffer, "FL:%d:%[^:]s:%d:%[^\n]s\n", &lID, lGUID, &lFileLength,
    lFileName);

    the lGUID gets read fine, but nothing after this gets read. Where can I
    find information about these scan types?
    Thanks
    Allan
     
    Allan Bruce, Apr 16, 2004
    #4
  5. "Allan Bruce" <> wrote:
    >>
    >> Warning, untested:
    >>
    >> sscanf( yourstring, "FL:%[^:]:%d:%[^\n]\n", lGuid, &lID, lFileName);
    >>
    >> If the ':'s might be surrounded by whitespace that should be ignored,
    >> you'll have to match and discard that, too.
    >>
    >> HTH
    >> Regards

    >
    >I tried:
    >sscanf(xiBuffer, "FL:%d:%[^:]s:%d:%[^\n]s\n", [....]

    ^ ^
    You've got two spurious s characters at the indicated positions.

    >the lGUID gets read fine, but nothing after this gets read. Where can I
    >find information about these scan types?


    Hm, RTFM? ;-)
    Seriously, your implementation should provide a documentation of the
    standard library functions. Alternatively, buy the C standard, or,
    if you're short on money, retrieve a copy of the final public draft
    for free at: http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/ .


    As a starter, here's an excerpt from the *scanf function description:

    ISO/IEC 9899:1999
    7.19.6.2 The fscanf function
    [...]

    [ Matches a nonempty sequence of characters from a set of expected
    characters (the scanset). If no l length modifier is present, the
    corresponding argument shall be a pointer to the initial element of
    a character array large enough to accept the sequence and a
    terminating null character, which will be added automatically.
    [...] The conversion specifier includes all subsequent characters
    in the format string, up to and including the matching right
    bracket (]). The characters between the brackets (the scanlist)
    compose the scanset, unless the character after the left bracket is
    a circumflex (^), in which case the scanset contains all characters
    that do not appear in the scanlist between the circumflex and the
    right bracket. [...]

    HTH
    Regards
    --
    Irrwahn Grausewitz ()
    welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
    clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
    clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
     
    Irrwahn Grausewitz, Apr 16, 2004
    #5
  6. Allan Bruce

    Dan Pop Guest

    In <c5o8ge$a8j$2surf.net> "Allan Bruce" <> writes:

    >If I have
    >sscanf("FL:%s:%d:%s\n", lGuid, &lID, lFileName);


    You're invoking undefined behaviour, because the input string is missing
    from your sscanf call.

    >and the last string contains spaces, e.g. my complete string
    >"FL:1234ABCD:3:FileName With Spaces.txt\n"
    >
    >does sscanf just make lFileName the string up to the whitespace?


    Yup.

    >even though I tell it the end of string as at the \n ?


    Nope, you're not telling it anything like that. You're merely telling
    it to eat *any* whitespace it finds after the string (which is a noop in
    your case, since you're using sscanf and not [f]scanf). There is NO way
    to tell %s that the first white space encountered is not ending the input
    string. You have to use a more sophisticated conversion descriptor for
    this purpose:

    #define INPUT "FL:1234ABCD:3:FileName With Spaces.txt\n"

    int rc = sscanf(INPUT, "FL:%s:%d:%[^\n]", lGuid, &lID, lFileName);

    Things to remember:

    1. If lFileName is not large enough to hold the whole first argument of
    the sscanf call, use an explicit maximum input field specification; no
    need to rediscover the joys of gets. This also applies to %s.

    2. Unlike most conversion specifications, including %s, %[ doesn't skip
    any preceding white space (just like %c). If needed, use an explicit
    white space in the format string, for this purpose (it is harmless if
    there is no white space at all at that point in the input string).

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Apr 16, 2004
    #6
  7. Allan Bruce

    Allan Bruce Guest


    > >I tried:
    > >sscanf(xiBuffer, "FL:%d:%[^:]s:%d:%[^\n]s\n", [....]

    > ^ ^
    > You've got two spurious s characters at the indicated positions.
    >
    > >the lGUID gets read fine, but nothing after this gets read. Where can I
    > >find information about these scan types?

    >
    > Hm, RTFM? ;-)
    > Seriously, your implementation should provide a documentation of the
    > standard library functions. Alternatively, buy the C standard, or,
    > if you're short on money, retrieve a copy of the final public draft
    > for free at: http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/ .
    >
    >


    Thanks, I have it working now.
    Allan
     
    Allan Bruce, Apr 16, 2004
    #7
  8. Allan Bruce

    Dan Pop Guest

    In <c5om52$raf$> I wrote:

    >In <c5o8ge$a8j$2surf.net> "Allan Bruce" <> writes:
    >
    >>If I have
    >>sscanf("FL:%s:%d:%s\n", lGuid, &lID, lFileName);

    >
    >You're invoking undefined behaviour, because the input string is missing
    >from your sscanf call.
    >
    >>and the last string contains spaces, e.g. my complete string
    >>"FL:1234ABCD:3:FileName With Spaces.txt\n"
    >>
    >>does sscanf just make lFileName the string up to the whitespace?

    >
    >Yup.
    >
    >>even though I tell it the end of string as at the \n ?

    >
    >Nope, you're not telling it anything like that. You're merely telling
    >it to eat *any* whitespace it finds after the string (which is a noop in
    >your case, since you're using sscanf and not [f]scanf). There is NO way
    >to tell %s that the first white space encountered is not ending the input
    >string. You have to use a more sophisticated conversion descriptor for
    >this purpose:
    >
    > #define INPUT "FL:1234ABCD:3:FileName With Spaces.txt\n"
    >
    > int rc = sscanf(INPUT, "FL:%s:%d:%[^\n]", lGuid, &lID, lFileName);


    Which is plagued by the problem already mentioned downthread by Allan:
    %s will eat too much, stopping after FileName. So, %s is not the right
    thing for lGuid, either:

    int rc = sscanf(INPUT, "FL:%[^:]:%d:%[^\n]", lGuid, &lID, lFileName);

    NEVER omit to check rc, to see if all the fields have been properly
    converted.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Apr 16, 2004
    #8

  9. > > If I have
    > > sscanf("FL:%s:%d:%s\n", lGuid, &lID, lFileName);

    ^

    That's a C++ reference operator isn't it? I don't think that has
    anything to do with a pointer. It is a COM Interface ID isn't it?

    Bill
     
    Bill Cunningham, Apr 18, 2004
    #9
  10. > the lGUID gets read fine, but nothing after this gets read. Where can I
    > find information about these scan types?
    > Thanks
    > Allan
    >

    sscanf reads a string instead of input from a keyboard.
    http://www-ccs.ucsd.edu/c/

    Bill
     
    Bill Cunningham, Apr 18, 2004
    #10
  11. Allan Bruce

    Allan Bruce Guest

    "Bill Cunningham" <> wrote in message
    news:...
    >
    > > > If I have
    > > > sscanf("FL:%s:%d:%s\n", lGuid, &lID, lFileName);

    > ^
    >
    > That's a C++ reference operator isn't it? I don't think that has
    > anything to do with a pointer. It is a COM Interface ID isn't it?
    >
    > Bill
    >
    >


    no, sscanf requires a pointer to write to, this is just an integer declared
    the line above as :
    int lID;
    so the & is taking the address of it to make a pointer on the fly.
    Allan
     
    Allan Bruce, Apr 18, 2004
    #11
  12. > no, sscanf requires a pointer to write to, this is just an integer
    declared
    > the line above as :
    > int lID;
    > so the & is taking the address of it to make a pointer on the fly.
    > Allan
    >

    Oh I see. that IID just looked like an interface identifier.

    Bill
     
    Bill Cunningham, Apr 18, 2004
    #12
    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. Anonymous
    Replies:
    0
    Views:
    512
    Anonymous
    Oct 30, 2003
  2. pramod
    Replies:
    22
    Views:
    1,971
    Lew Pitcher
    Jan 6, 2004
  3. Mike

    sscanf and c++!

    Mike, Jun 8, 2004, in forum: C++
    Replies:
    3
    Views:
    3,596
  4. Matt

    sscanf feature in C++?

    Matt, Oct 18, 2004, in forum: C++
    Replies:
    15
    Views:
    19,097
    Default User
    Oct 19, 2004
  5. Darrell Grainger

    Re: Unitialized Memory Read In sscanf()

    Darrell Grainger, Jun 24, 2003, in forum: C Programming
    Replies:
    1
    Views:
    837
    Alan Balmer
    Jun 24, 2003
Loading...

Share This Page