how to use sscanf?

Discussion in 'C Programming' started by nick, Nov 30, 2005.

  1. nick

    nick Guest

    is it similar to scanf?
    when i use scanf it can read the words in the screen automatically one
    after another.i use a char array to store the string,then use sscanf to
    read the words,but it just only reat out the first word in the string
    array every time. so if i want to read the words in the string one by
    one, just like scanf, what should i do?

    thanks!
     
    nick, Nov 30, 2005
    #1
    1. Advertising

  2. nick

    Saif Guest

    nick wrote:
    > is it similar to scanf?
    > when i use scanf it can read the words in the screen automatically one
    > after another.i use a char array to store the string,then use sscanf to
    > read the words,but it just only reat out the first word in the string
    > array every time. so if i want to read the words in the string one by
    > one, just like scanf, what should i do?


    If all you're going to read is words (strings), you can increment the
    buffer pointer passed to sscanf by the length of the string previously
    read.

    Assuming your complete string is pointed to by char *str and you want
    to read each word from it into another already allocated character
    array char *word. Then...

    /*Read first word*/
    sscanf(str,"%s", word);
    /*Read next word*/
    sscanf(str+strlen(word), "%s", word);

    You can repeat this to read consecutive strings.

    sscanf is typically used though when you know exactly the number and
    type of fields you want to extract from the parent string. You will
    then use sscanf just once and get all the fields in one shot instead of
    having to manipulate the buffer pointer for each field you read.
     
    Saif, Nov 30, 2005
    #2
    1. Advertising

  3. nick

    Becker Guest

    More basically,

    The scanf() prototype is:
    /*
    ---------------------------------------------------------------------------------------------
    */
    int scanf ( const char * format [ , argument , ...] );

    The sscanf() prototype is:
    /*
    ---------------------------------------------------------------------------------------------
    */
    int sscanf ( char * buffer, const char * format [ , argument , ...] );


    I guess you must be familiar with the function scanf(), which reads
    data from the standard input(stdin) and stores it to the locations
    specified by the argument(s) passed to it(scanf()).
    sscanf() reads data from the specified -buffer- and stores it into the
    locations by the argument(s).

    Just as what Saif said:
    > sscanf is typically used though when you know exactly the number and
    > type of fields you want to extract from the parent string. You will
    > then use sscanf just once and get all the fields in one shot instead of
    > having to manipulate the buffer pointer for each field you read.


    /* BEGING */
    #include <stdio.h>

    int main (void)
    {
    char specifiedBuffer[] = "C is a good programming language!";
    char str1[10], str2[10];
    int i;

    sscanf (specifiedBuffer, "%s %*s %*s %s", str1, str2);
    printf ("%s => %s\n", str1, str2);

    return 0;
    }

    /* END */

    Output:
    C => good

    /*
    Becker
    */
     
    Becker, Nov 30, 2005
    #3
  4. nick

    Becker Guest

    More basically,

    The scanf() prototype is:
    /* ------------------------------------------------­------------------
    */
    int scanf ( const char * format [ , argument , ...] );

    The sscanf() prototype is:
    /* ------------------------------------------------­------------------
    */
    int sscanf ( char * buffer, const char * format [ , argument , ...] );



    I guess you must be familiar with the function scanf(), which reads
    data from the standard input(stdin) and stores it to the locations
    specified by the argument(s) passed to it(scanf()).
    sscanf() reads data from the specified -buffer- and stores it into the
    locations by the argument(s).

    Just as what Saif said:

    > sscanf is typically used though when you know exactly the number and
    > type of fields you want to extract from the parent string. You will
    > then use sscanf just once and get all the fields in one shot instead of
    > having to manipulate the buffer pointer for each field you read.



    /* BEGING */
    #include <stdio.h>

    int main (void)
    {
    char specifiedBuffer[] = "C is a good programming language!";
    char str1[10], str2[10];

    sscanf (specifiedBuffer, "%s %*s %*s %s", str1, str2);
    printf ("%s => %s\n", str1, str2);

    return 0;
    }

    /* END */

    Output:
    C => good


    /*
    Becker
    */
     
    Becker, Nov 30, 2005
    #4
  5. nick

    Suman Guest

    Becker wrote:
    > More basically,
    >
    > The scanf() prototype is:
    > /* ------------------------------------------------­------------------
    > */
    > int scanf ( const char * format [ , argument , ...] );


    I have:
    7.19.6.4 The scanf function
    Synopsis
    1 #include <stdio.h>
    int scanf(const char * restrict format, ...);

    > The sscanf() prototype is:
    > /* ------------------------------------------------­------------------
    > */
    > int sscanf ( char * buffer, const char * format [ , argument , ...] );
    >

    I have:
    7.19.6.7 The sscanf function
    Synopsis
    1 #include <stdio.h>
    int sscanf(const char * restrict s,
    const char * restrict format, ...);

    > /* BEGING */


    :)

    > #include <stdio.h>
    >
    > int main (void)
    > {
    > char specifiedBuffer[] = "C is a good programming language!";

    And what happens when there's a typo like:
    char unspecifiedBuffer[] = "pooooorprogramming language!";
    > char str1[10], str2[10];
    >
    > sscanf (specifiedBuffer, "%s %*s %*s %s", str1, str2);

    sscanf (unspecifiedBuffer, "%s %*s %*s %s", str1, str2);

    Ought to check return values of *scanf() and friends...

    [snip]
     
    Suman, Nov 30, 2005
    #5
  6. nick

    nick Guest

    thanks yours reply, but i have another question, the question is, how to
    check where is the end of the string?

    thanks!
     
    nick, Nov 30, 2005
    #6
  7. nick

    Jordan Abel Guest

    On 2005-11-30, nick <> wrote:
    > thanks yours reply, but i have another question, the question is, how to
    > check where is the end of the string?


    strlen(str) returns an index, strchr(str,0) a pointer; pick your poison.
     
    Jordan Abel, Nov 30, 2005
    #7
  8. On 30 Nov 2005 03:03:18 -0800, "Saif" <> wrote:

    > nick wrote:
    > > <snip> so if i want to read the words in the string one by
    > > one, just like scanf, what should i do?

    >
    > If all you're going to read is words (strings), you can increment the
    > buffer pointer passed to sscanf by the length of the string previously
    > read.
    >
    > Assuming your complete string is pointed to by char *str and you want
    > to read each word from it into another already allocated character
    > array char *word. Then...
    >
    > /*Read first word*/
    > sscanf(str,"%s", word);
    > /*Read next word*/
    > sscanf(str+strlen(word), "%s", word);
    >
    > You can repeat this to read consecutive strings.
    >

    %s _skips whitespace_ and then reads a string of non-whitespace, i.e.
    a "word". Or fails due to hitting the end of the input, which you
    should catch by checking the return value, as you should for all
    *scanf variants. strlen(word) doesn't allow for the whitespace. If
    there is leading whitespace in the input string, this will (first)
    fail for second word; otherwise for the third.

    Also, %s (and %[) in *scanf should always be given with a length
    limit, to prevent either accidentally or maliciously exceeding the
    actual object (buffer) size and causing Undefined Behavior, which in
    the latter (malicious) case is likely to be destroying your data
    and/or stealing your money. Unless you are absolutely 100% sure the
    input is valid, which in practice is only if was generated by a valid
    sprintf call or similar in the line immediately preceding the sscanf
    call, in which case you already have the data and don't need to scan.

    Try:
    char * ptr = str; /* if not already a pointer you can spare */
    int used;
    if( sscanf (ptr, "%Ns%n", word, &used) < 1 ) /* error */
    ptr += used;
    if( sscan (ptr, ...

    - David.Thompson1 at worldnet.att.net
     
    Dave Thompson, Dec 14, 2005
    #8
    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:
    503
    Anonymous
    Oct 30, 2003
  2. pramod
    Replies:
    22
    Views:
    1,942
    Lew Pitcher
    Jan 6, 2004
  3. Mike

    sscanf and c++!

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

    sscanf feature in C++?

    Matt, Oct 18, 2004, in forum: C++
    Replies:
    15
    Views:
    19,075
    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:
    833
    Alan Balmer
    Jun 24, 2003
Loading...

Share This Page