Scanset

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

    #1

    Scanset

    I can't find a good charset for my string.
    The separator is ':' :-(

    Look at this code

    int main()
    {
    char *str = "titititi:tutu: toto\n";
    char buf1[64];
    char buf2[64];
    char buf3[64];

    sscanf(str, "%64s[:]%64s[:]%64s[\n]", buf1, buf2, buf3);
    printf("buf1: %s\n", buf1);
    printf("buf2: %s\n", buf2);
    printf("buf3: %s\n", buf3);
    }

    I would like to have tititi in buf1, tutu in buf2 and toto in buf3
    But with this scanset It doesn't work. I have tried
    "%64s%*[:]%64s%*[:]%64s%*[\n]" and It doesn't work too.

    Could someone help me? :]

    --
    Lezard

  • pete

    #2
    Re: Scanset

    lezard wrote:[color=blue]
    >
    > I can't find a good charset for my string.
    > The separator is ':' :-(
    >
    > Look at this code
    >
    > int main()
    > {
    > char *str = "titititi:tutu: toto\n";
    > char buf1[64];
    > char buf2[64];
    > char buf3[64];
    >
    > sscanf(str, "%64s[:]%64s[:]%64s[\n]", buf1, buf2, buf3);
    > printf("buf1: %s\n", buf1);
    > printf("buf2: %s\n", buf2);
    > printf("buf3: %s\n", buf3);
    > }
    >
    > I would like to have tititi in buf1, tutu in buf2 and toto in buf3
    > But with this scanset It doesn't work. I have tried
    > "%64s%*[:]%64s%*[:]%64s%*[\n]" and It doesn't work too.
    >
    > Could someone help me? :][/color]

    int main(void)
    {
    char *str = "titititi:tutu: toto\n";
    char buf1[64];
    char buf2[64];
    char buf3[64];

    sscanf(str, "%64[^:]%*[:]%64[^:]%*[:]%64[^\n]", buf1, buf2, buf3);
    printf("buf1: %s\n", buf1);
    printf("buf2: %s\n", buf2);
    printf("buf3: %s\n", buf3);
    return 0;
    }

    --
    pete

    Comment

    • lezard

      #3
      Re: Scanset

      > int main(void)[color=blue]
      > {
      > char *str = "titititi:tutu: toto\n";
      > char buf1[64];
      > char buf2[64];
      > char buf3[64];
      >
      > sscanf(str, "%64[^:]%*[:]%64[^:]%*[:]%64[^\n]", buf1, buf2, buf3);
      > printf("buf1: %s\n", buf1);
      > printf("buf2: %s\n", buf2);
      > printf("buf3: %s\n", buf3);
      > return 0;
      > }
      >[/color]

      Thx a lot Pete!

      Comment

      • Eric Sosman

        #4
        Re: Scanset

        pete wrote:
        [color=blue]
        > lezard wrote:
        >[color=green]
        >>I can't find a good charset for my string.
        >>The separator is ':' :-(
        >>
        >>Look at this code
        >>
        >>int main()
        >>{
        >> char *str = "titititi:tutu: toto\n";
        >> char buf1[64];
        >> char buf2[64];
        >> char buf3[64];
        >>
        >> sscanf(str, "%64s[:]%64s[:]%64s[\n]", buf1, buf2, buf3);
        >> printf("buf1: %s\n", buf1);
        >> printf("buf2: %s\n", buf2);
        >> printf("buf3: %s\n", buf3);
        >>}
        >>
        >>I would like to have tititi in buf1, tutu in buf2 and toto in buf3
        >>But with this scanset It doesn't work. I have tried
        >>"%64s%*[:]%64s%*[:]%64s%*[\n]" and It doesn't work too.
        >>
        >>Could someone help me? :][/color]
        >
        >
        > int main(void)
        > {
        > char *str = "titititi:tutu: toto\n";
        > char buf1[64];
        > char buf2[64];
        > char buf3[64];
        >
        > sscanf(str, "%64[^:]%*[:]%64[^:]%*[:]%64[^\n]", buf1, buf2, buf3);[/color]

        ITYM "63" for each "64". Also, the format can be
        simplified a bit:

        sscanf(str, "%63[^:]:%63[^:]:%63[^\n]", ...

        --
        Eric Sosman
        esosman@acm-dot-org.invalid

        Comment

        • Peter Nilsson

          #5
          Re: Scanset

          pete wrote:[color=blue]
          > ...
          > int main(void)
          > {
          > char *str = "titititi:tutu: toto\n";
          > char buf1[64];
          > char buf2[64];
          > char buf3[64];
          >
          > sscanf(str, "%64[^:]%*[:]%64[^:]%*[:]%64[^\n]", buf1, buf2,[/color]
          buf3);

          Note you should check the return value of sscanf. For instance, the
          string "a::c" will put "a" into buf1, and "c" into buf2, with sscanf
          returning 2, leaving the contents of buf3 unspecified. You also have
          an off by 1 error.

          You can also simplify by matching the colons directly...

          r = sscanf(str, "%63[^:\n]:%63[^:\n]:%63[^\n]", buf1, buf2, buf3);
          [color=blue]
          > printf("buf1: %s\n", buf1);
          > printf("buf2: %s\n", buf2);
          > printf("buf3: %s\n", buf3);
          > return 0;
          > }[/color]

          --
          Peter

          Comment

          • pete

            #6
            Re: Scanset

            Eric Sosman wrote:[color=blue]
            >
            > pete wrote:
            >[color=green]
            > > lezard wrote:
            > >[color=darkred]
            > >>I can't find a good charset for my string.
            > >>The separator is ':' :-(
            > >>
            > >>Look at this code
            > >>
            > >>int main()
            > >>{
            > >> char *str = "titititi:tutu: toto\n";
            > >> char buf1[64];
            > >> char buf2[64];
            > >> char buf3[64];
            > >>
            > >> sscanf(str, "%64s[:]%64s[:]%64s[\n]", buf1, buf2, buf3);
            > >> printf("buf1: %s\n", buf1);
            > >> printf("buf2: %s\n", buf2);
            > >> printf("buf3: %s\n", buf3);
            > >>}
            > >>
            > >>I would like to have tititi in buf1, tutu in buf2 and toto in buf3
            > >>But with this scanset It doesn't work. I have tried
            > >>"%64s%*[:]%64s%*[:]%64s%*[\n]" and It doesn't work too.
            > >>
            > >>Could someone help me? :][/color]
            > >
            > >
            > > int main(void)
            > > {
            > > char *str = "titititi:tutu: toto\n";
            > > char buf1[64];
            > > char buf2[64];
            > > char buf3[64];
            > >
            > > sscanf(str, "%64[^:]%*[:]%64[^:]%*[:]%64[^\n]",
            > > buf1, buf2, buf3);[/color]
            >
            > ITYM "63" for each "64".[/color]

            I think so too.
            [color=blue]
            > Also, the format can be
            > simplified a bit:
            >
            > sscanf(str, "%63[^:]:%63[^:]:%63[^\n]", ...[/color]

            Thank you.

            --
            pete

            Comment

            • pete

              #7
              Re: Scanset

              Peter Nilsson wrote:[color=blue]
              >
              > pete wrote:[color=green]
              > > ...
              > > int main(void)
              > > {
              > > char *str = "titititi:tutu: toto\n";
              > > char buf1[64];
              > > char buf2[64];
              > > char buf3[64];
              > >
              > > sscanf(str, "%64[^:]%*[:]%64[^:]%*[:]%64[^\n]", buf1, buf2,[/color]
              > buf3);
              >
              > Note you should check the return value of sscanf. For instance, the
              > string "a::c" will put "a" into buf1, and "c" into buf2, with sscanf
              > returning 2, leaving the contents of buf3 unspecified. You also have
              > an off by 1 error.
              >
              > You can also simplify by matching the colons directly...
              >
              > r = sscanf(str, "%63[^:\n]:%63[^:\n]:%63[^\n]", buf1, buf2, buf3);[/color]

              Thank you.

              --
              pete

              Comment

              • CBFalconer

                #8
                Re: Scanset

                lezard wrote:[color=blue]
                >[/color]
                .... snip ...[color=blue]
                > {
                > char *str = "titititi:tutu: toto\n";
                >[/color]
                .... snip ...[color=blue]
                >
                > I would like to have tititi in buf1, tutu in buf2 and toto in buf3[/color]

                Many people would like to see all titis in the buf. Those with
                tutus are innately not in the buf, and I think toto would bark if
                so confined. Meanwhile you are offending the prudes :-)

                --
                Some informative links:
                news:news.annou nce.newusers
                Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!






                Comment

                • Richard Bos

                  #9
                  Re: Scanset

                  CBFalconer <cbfalconer@yah oo.com> wrote:
                  [color=blue]
                  > lezard wrote:[color=green]
                  > > I would like to have tititi in buf1, tutu in buf2 and toto in buf3[/color]
                  >
                  > Many people would like to see all titis in the buf. Those with
                  > tutus are innately not in the buf, and I think toto would bark if
                  > so confined. Meanwhile you are offending the prudes :-)[/color]

                  ....which is always a good thing to do ;->

                  Richard

                  Comment

                  Working...