scanf() help

Discussion in 'C Programming' started by Stu, Oct 7, 2004.

  1. Stu

    Stu Guest

    I have the following "C" program, which works fine.

    #include <stdio.h>
    #include <stdlib.h>
    #include <strings.h>

    int
    main()
    {
    char *buffer = "1234 - 5678";
    int n, from, to;

    n = sscanf (buffer, "%d%*s%d", &from, &to);

    printf ("n = %d from = %d, to = %d\n", n, from, to);

    return (0);
    }

    When I change the vaule of buffer to char *buffer = "1234-5678"; (note
    I removed the spaces between '-') I need to change my scanf() to

    n = sscanf (buffer, "%d%*c%d", &from, &to); /*change from %s to %c */

    Can somebody please provide me with one scanf() statement that can handle
    both spaces and non spaces between my values. The man page on Solaris did not
    provide me with much direction thats why I am posting my question here.


    Thanks in advance to all that answer
    Stu, Oct 7, 2004
    #1
    1. Advertising

  2. Stu

    Eric Sosman Guest

    Stu wrote:
    > I have the following "C" program, which works fine.
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <strings.h>
    >
    > int
    > main()
    > {
    > char *buffer = "1234 - 5678";
    > int n, from, to;
    >
    > n = sscanf (buffer, "%d%*s%d", &from, &to);
    >
    > printf ("n = %d from = %d, to = %d\n", n, from, to);
    >
    > return (0);
    > }
    >
    > When I change the vaule of buffer to char *buffer = "1234-5678"; (note
    > I removed the spaces between '-') I need to change my scanf() to
    >
    > n = sscanf (buffer, "%d%*c%d", &from, &to); /*change from %s to %c */
    >
    > Can somebody please provide me with one scanf() statement that can handle
    > both spaces and non spaces between my values. The man page on Solaris did not
    > provide me with much direction thats why I am posting my question here.


    It sounds like you need "%*[- ]" (perhaps with additional
    "don't care" characters listed). Note that since '-' is one
    of the characters you want to skip, there'll be no way to get
    a negative `to' value.

    --
    Eric Sosman, Oct 7, 2004
    #2
    1. Advertising

  3. Stu

    Chris Torek Guest

    >Stu wrote:
    [snippage]
    >> char *buffer = "1234 - 5678";
    >> int n, from, to;
    >>
    >> n = sscanf (buffer, "%d%*s%d", &from, &to);

    [or]
    >> When I change the vaule of buffer to char *buffer = "1234-5678"; ...
    >> n = sscanf (buffer, "%d%*c%d", &from, &to); /*change from %s to %c */
    >>
    >> Can somebody please provide me with one scanf() statement that can handle
    >> both spaces and non spaces between my values. ...


    In article <news:ck44no$e8i$>
    Eric Sosman <> wrote:
    > It sounds like you need "%*[- ]" (perhaps with additional
    >"don't care" characters listed). Note that since '-' is one
    >of the characters you want to skip, there'll be no way to get
    >a negative `to' value.


    This will do the trick, but note that the %[ directive matches
    *any* nonempty sequence of *any* of the characters in the scanset
    (here just '-' and ' '), so this matches:

    1234---5678

    and:

    1234 - -- - 5678

    and:

    1234- -5678

    (which, as Eric Sosman noted, means there will be no negative "to"
    values).

    Another alternative is to use the scanf engine's white-space-matching
    directives, e.g.:

    n = sscanf(buffer, "%d\t%*c\b%d", &from, &to); /* odd way to write it */

    Here the whitespace in the format -- \t and \b characters, which
    is why it is odd since most people would just use a blank -- tells
    scanf to read and ignore zero or more white-space characters in
    the input (the string in the buffer, in this case).

    The second whitespace directive is actually redundant, because %d
    skips initial whitespace automatically anyway, so a more minimal
    (and less odd) form is:

    n = sscanf(buffer, "%d %*c%d", &from, &to);

    If you would like to *require* that the single eaten-up character
    be a hyphen, you can just put in a literal hyphen:

    n = sscanf(buffer, "%d -%d", &from, &to);

    The blank after the first "%d" reads and discards any arbitrary
    amount of whitespace that follows the first "%d" conversion, the
    "-" then matches only a literal '-' character, and the second %d
    reads the second number (first reading and discarding any arbitrary
    amount of whitespace, as usual). A final, more-symmetrical looking
    variant is:

    n = sscanf(buffer, "%d - %d", &from, &to);

    which redundantly (but harmlessly) asks the scanf engine to skip
    whitespace twice.
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
    Chris Torek, Oct 8, 2004
    #3
    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. Ramprasad A Padmanabhan

    scanf help again

    Ramprasad A Padmanabhan, Jul 24, 2003, in forum: C Programming
    Replies:
    3
    Views:
    416
    Dave Thompson
    Aug 8, 2003
  2. Rob

    help with infinite loops and scanf

    Rob, Jul 26, 2003, in forum: C Programming
    Replies:
    8
    Views:
    754
    Peter Shaggy Haywood
    Jul 29, 2003
  3. Sivarn

    Help with scanf

    Sivarn, Apr 1, 2004, in forum: C Programming
    Replies:
    23
    Views:
    1,252
    Dan Pop
    Apr 13, 2004
  4. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc.

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 16, 2006, in forum: C Programming
    Replies:
    185
    Views:
    3,311
    those who know me have no need of my name
    Apr 3, 2006
  5. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005?

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Apr 26, 2006, in forum: C Programming
    Replies:
    18
    Views:
    650
    Richard Bos
    May 2, 2006
Loading...

Share This Page