sscanf

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Allan Bruce

    #1

    sscanf

    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

    #2
    Re: sscanf


    "Allan Bruce" <allanmb@TAKEAW AYf2s.com> wrote in message
    news:c5o8ge$a8j $1@news.freedom 2surf.net...[color=blue]
    > 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[/color]
    though[color=blue]
    > I tell it the end of string as at the \n ?
    >
    > Thanks
    > Allan
    >
    >[/color]

    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


    Comment

    • Irrwahn Grausewitz

      #3
      Re: sscanf

      "Allan Bruce" <allanmb@TAKEAW AYf2s.com> wrote:[color=blue]
      >
      >"Allan Bruce" <allanmb@TAKEAW AYf2s.com> wrote:[color=green]
      >> If I have
      >> sscanf("FL:%s:% d:%s\n", lGuid, &lID, lFileName);[/color][/color]
      ^
      You forgot to specify the string to parse
      [color=blue][color=green]
      >> 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[/color]
      >though[color=green]
      >> I tell it the end of string as at the \n ?[/color]
      >
      >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[/color]

      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 (irrwahn33@free net.de)
      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

      Comment

      • Allan Bruce

        #4
        Re: sscanf

        >[color=blue]
        > 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[/color]

        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


        Comment

        • Irrwahn Grausewitz

          #5
          Re: sscanf

          "Allan Bruce" <allanmb@TAKEAW AYf2s.com> wrote:[color=blue][color=green]
          >>
          >> 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[/color]
          >
          >I tried:
          >sscanf(xiBuffe r, "FL:%d:%[^:]s:%d:%[^\n]s\n", [....][/color]
          ^ ^
          You've got two spurious s characters at the indicated positions.
          [color=blue]
          >the lGUID gets read fine, but nothing after this gets read. Where can I
          >find information about these scan types?[/color]

          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 (irrwahn33@free net.de)
          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

          Comment

          • Allan Bruce

            #6
            Re: sscanf

            [color=blue][color=green]
            > >I tried:
            > >sscanf(xiBuffe r, "FL:%d:%[^:]s:%d:%[^\n]s\n", [....][/color]
            > ^ ^
            > You've got two spurious s characters at the indicated positions.
            >[color=green]
            > >the lGUID gets read fine, but nothing after this gets read. Where can I
            > >find information about these scan types?[/color]
            >
            > 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/ .
            >
            >[/color]

            Thanks, I have it working now.
            Allan


            Comment

            • Dan Pop

              #7
              Re: sscanf

              In <c5o8ge$a8j$1@n ews.freedom2sur f.net> "Allan Bruce" <allanmb@TAKEAW AYf2s.com> writes:
              [color=blue]
              >If I have
              >sscanf("FL:%s: %d:%s\n", lGuid, &lID, lFileName);[/color]

              You're invoking undefined behaviour, because the input string is missing
              from your sscanf call.
              [color=blue]
              >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?[/color]

              Yup.
              [color=blue]
              >even though I tell it the end of string as at the \n ?[/color]

              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@ifh.de

              Comment

              • Dan Pop

                #8
                Re: sscanf

                In <c5om52$raf$15@ sunnews.cern.ch > I wrote:
                [color=blue]
                >In <c5o8ge$a8j$1@n ews.freedom2sur f.net> "Allan Bruce" <allanmb@TAKEAW AYf2s.com> writes:
                >[color=green]
                >>If I have
                >>sscanf("FL:%s :%d:%s\n", lGuid, &lID, lFileName);[/color]
                >
                >You're invoking undefined behaviour, because the input string is missing
                >from your sscanf call.
                >[color=green]
                >>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?[/color]
                >
                >Yup.
                >[color=green]
                >>even though I tell it the end of string as at the \n ?[/color]
                >
                >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);[/color]

                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@ifh.de

                Comment

                • Bill Cunningham

                  #9
                  Re: sscanf

                  [color=blue][color=green]
                  > > If I have
                  > > sscanf("FL:%s:% d:%s\n", lGuid, &lID, lFileName);[/color][/color]
                  ^

                  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


                  Comment

                  • Bill Cunningham

                    #10
                    Re: sscanf

                    > the lGUID gets read fine, but nothing after this gets read. Where can I[color=blue]
                    > find information about these scan types?
                    > Thanks
                    > Allan
                    >[/color]
                    sscanf reads a string instead of input from a keyboard.


                    Bill


                    Comment

                    • Allan Bruce

                      #11
                      Re: sscanf


                      "Bill Cunningham" <nospam@nspam.n et> wrote in message
                      news:1085dtb3k0 9hl8f@corp.supe rnews.com...[color=blue]
                      >[color=green][color=darkred]
                      > > > If I have
                      > > > sscanf("FL:%s:% d:%s\n", lGuid, &lID, lFileName);[/color][/color]
                      > ^
                      >
                      > 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
                      >
                      >[/color]

                      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


                      Comment

                      • Bill Cunningham

                        #12
                        Re: sscanf

                        > no, sscanf requires a pointer to write to, this is just an integer
                        declared[color=blue]
                        > the line above as :
                        > int lID;
                        > so the & is taking the address of it to make a pointer on the fly.
                        > Allan
                        >[/color]
                        Oh I see. that IID just looked like an interface identifier.

                        Bill



                        Comment

                        Working...