Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > sscanf style string parsing

Reply
Thread Tools

sscanf style string parsing

 
 
Markus Ilmola
Guest
Posts: n/a
 
      03-10-2006
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).
 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      03-10-2006

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.

 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      03-10-2006
In article <BkmQf.562$(E-Mail Removed)>,
Markus Ilmola <(E-Mail Removed)> 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.
 
Reply With Quote
 
Micah Cowan
Guest
Posts: n/a
 
      03-11-2006
"Daniel T." <(E-Mail Removed)> 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.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      03-11-2006
Micah Cowan wrote:
> "Daniel T." <(E-Mail Removed)> 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


 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      03-11-2006
In article <(E-Mail Removed)>,
Micah Cowan <(E-Mail Removed)> wrote:

> "Daniel T." <(E-Mail Removed)> 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.
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      03-11-2006
Victor Bazarov wrote:

> Micah Cowan wrote:
> >"Daniel T." <(E-Mail Removed)> 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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple sscanf parsing problem Timo C Programming 4 06-28-2008 02:11 PM
How to create a format string used by eg sscanf devrobcom@hotmail.com C Programming 3 06-08-2006 02:11 AM
sscanf parsing doubt Simone Mehta C Programming 12 10-03-2004 09:04 AM
sscanf parsing doubt Simone Mehta C Programming 0 09-27-2004 02:59 AM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM



Advertisments