sscanf style string parsing

Discussion in 'C++' started by Markus Ilmola, Mar 10, 2006.

  1. How to a parse a string using C++ (standard library) same way as sscanf
    in C.

    For example if a have a string:

    My name is "John Smith" and I'm 13 years old and 120 cm tall.

    and a want to parse the name (string that can be empty (whitout the
    quotation marks)), age (unsigned int) and height (unsigned int).

    Using plain C I could write something like this:

    char name[64];
    unsigned int age, height;
    sscanf(buffer, "My name is \"%[^\"]\" and I'm %u years old and %u cm
    tall.");

    However I want to use C++ (standard library) and I need to read the name
    to a std::string (buffer is also std::string).
     
    Markus Ilmola, Mar 10, 2006
    #1
    1. Advertising

  2. Markus Ilmola

    Noah Roberts Guest

    Markus Ilmola wrote:
    > How to a parse a string using C++ (standard library) same way as sscanf
    > in C.
    >
    > For example if a have a string:
    >
    > My name is "John Smith" and I'm 13 years old and 120 cm tall.
    >
    > and a want to parse the name (string that can be empty (whitout the
    > quotation marks)), age (unsigned int) and height (unsigned int).
    >
    > Using plain C I could write something like this:
    >
    > char name[64];
    > unsigned int age, height;
    > sscanf(buffer, "My name is \"%[^\"]\" and I'm %u years old and %u cm
    > tall.");


    You would check for the existance of "My name is \"". Then you would
    pass the rest into an istringstream and try to read an int. etc,
    etc...

    Or you can use something much more powerful like spirit.

    You forgot to provide all the args to sscanf btw.
     
    Noah Roberts, Mar 10, 2006
    #2
    1. Advertising

  3. Markus Ilmola

    Daniel T. Guest

    In article <BkmQf.562$>,
    Markus Ilmola <> wrote:

    > How to a parse a string using C++ (standard library) same way as sscanf
    > in C.
    >
    > For example if a have a string:
    >
    > My name is "John Smith" and I'm 13 years old and 120 cm tall.
    >
    > and a want to parse the name (string that can be empty (whitout the
    > quotation marks)), age (unsigned int) and height (unsigned int).
    >
    > Using plain C I could write something like this:
    >
    > char name[64];
    > unsigned int age, height;
    > sscanf(buffer, "My name is \"%[^\"]\" and I'm %u years old and %u cm
    > tall.");
    >
    > However I want to use C++ (standard library) and I need to read the name
    > to a std::string (buffer is also std::string).


    The following causes my compiler to barf:

    int main()
    {
    char buffer[] = "My name is \"John Smith\" and I'm 13 years old and
    120 cm tall.";
    char name[64];
    unsigned int age, height;
    sscanf(buffer, "My name is \"%[^\"]\" and I'm %u years old and %u cm
    tall.", name, age, height);
    }

    Are you sure you can do that with sscanf?

    I'd say the easiest way to do it in C++ would be something like this:

    void fn( istream& is, string& name, int& age, int& height )
    {
    find( istream_iterator<char>( is ), istream_iterator<char>(), '\"' );
    getline( is, name, '\"' );
    find( istream_iterator<string>( is ), istream_iterator<string>(),
    "I'm" );
    is >> age;
    find( istream_iterator<string>( is ), istream_iterator<string>(),
    "and" );
    is >> height;
    }


    --
    Magic depends on tradition and belief. It does not welcome observation,
    nor does it profit by experiment. On the other hand, science is based
    on experience; it is open to correction by observation and experiment.
     
    Daniel T., Mar 10, 2006
    #3
  4. Markus Ilmola

    Micah Cowan Guest

    "Daniel T." <> writes:

    > The following causes my compiler to barf:
    >
    > int main()
    > {
    > char buffer[] = "My name is \"John Smith\" and I'm 13 years old and
    > 120 cm tall.";
    > char name[64];
    > unsigned int age, height;
    > sscanf(buffer, "My name is \"%[^\"]\" and I'm %u years old and %u cm
    > tall.", name, age, height);
    > }


    Probably because you forgot the & before age and before height.
     
    Micah Cowan, Mar 11, 2006
    #4
  5. Micah Cowan wrote:
    > "Daniel T." <> writes:
    >
    >> The following causes my compiler to barf:
    >>
    >> int main()
    >> {
    >> char buffer[] = "My name is \"John Smith\" and I'm 13 years old
    >> and 120 cm tall.";
    >> char name[64];
    >> unsigned int age, height;
    >> sscanf(buffer, "My name is \"%[^\"]\" and I'm %u years old and %u
    >> cm tall.", name, age, height);
    >> }

    >
    > Probably because you forgot the & before age and before height.


    That wouldn't make the compiler barf. It would cause undefined behaviour.
    'sscanf' has ... where the 'age' and 'height' are, and the compiler could
    not really care less whether you supply the values or the addresses of
    them.

    V
    --
    Please remove capital As from my address when replying by mail
     
    Victor Bazarov, Mar 11, 2006
    #5
  6. Markus Ilmola

    Daniel T. Guest

    In article <>,
    Micah Cowan <> wrote:

    > "Daniel T." <> writes:
    >
    > > The following causes my compiler to barf:
    > >
    > > int main()
    > > {
    > > char buffer[] = "My name is \"John Smith\" and I'm 13 years old and
    > > 120 cm tall.";
    > > char name[64];
    > > unsigned int age, height;
    > > sscanf(buffer, "My name is \"%[^\"]\" and I'm %u years old and %u cm
    > > tall.", name, age, height);
    > > }

    >
    > Probably because you forgot the & before age and before height.


    Yea, that was pretty stupid of me. Just goes to show how often I use the
    c io functions...

    --
    Magic depends on tradition and belief. It does not welcome observation,
    nor does it profit by experiment. On the other hand, science is based
    on experience; it is open to correction by observation and experiment.
     
    Daniel T., Mar 11, 2006
    #6
  7. Markus Ilmola

    Default User Guest

    Victor Bazarov wrote:

    > Micah Cowan wrote:
    > >"Daniel T." <> writes:
    > >
    > > > The following causes my compiler to barf:
    > > >
    > > > int main()
    > > > {
    > >> char buffer[] = "My name is \"John Smith\" and I'm 13 years old
    > > > and 120 cm tall.";
    > >> char name[64];
    > >> unsigned int age, height;
    > >> sscanf(buffer, "My name is \"%[^\"]\" and I'm %u years old and %u
    > > > cm tall.", name, age, height);
    > > > }

    > >
    > > Probably because you forgot the & before age and before height.

    >
    > That wouldn't make the compiler barf. It would cause undefined
    > behaviour. 'sscanf' has ... where the 'age' and 'height' are, and
    > the compiler could not really care less whether you supply the values
    > or the addresses of them.



    Might not care. There's nothing to keep the compiler from analyzing the
    arguments to the scanf() family, many do, it's just not required to do
    so.

    For instance, compiled with gcc and -Wall:

    > sscf.c: In function `main':
    > sscf.c:10: warning: format argument is not a pointer (arg 4)
    > sscf.c:10: warning: format argument is not a pointer (arg 5)




    Brian
    --
    If televison's a babysitter, the Internet is a drunk librarian who
    won't shut up.
    -- Dorothy Gambrell (http://catandgirl.com)
     
    Default User, Mar 11, 2006
    #7
    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. Simone Mehta

    sscanf parsing doubt

    Simone Mehta, Sep 26, 2004, in forum: C Programming
    Replies:
    12
    Views:
    730
    Aslam Sheikh Durrani
    Oct 3, 2004
  2. Simone Mehta

    sscanf parsing doubt

    Simone Mehta, Sep 27, 2004, in forum: C Programming
    Replies:
    0
    Views:
    319
    Simone Mehta
    Sep 27, 2004
  3. Replies:
    3
    Views:
    424
    Dave Thompson
    Jun 8, 2006
  4. Timo

    Simple sscanf parsing problem

    Timo, Jun 28, 2008, in forum: C Programming
    Replies:
    4
    Views:
    449
    CBFalconer
    Jun 28, 2008
  5. Ken Varn
    Replies:
    0
    Views:
    493
    Ken Varn
    Apr 26, 2004
Loading...

Share This Page