473,387 Members | 1,476 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

sscanf style string parsing

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).
Mar 10 '06 #1
6 4184

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.

Mar 10 '06 #2
In article <Bk***************@read3.inet.fi>,
Markus Ilmola <ma***********@pp.inet.fi> 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.
Mar 11 '06 #3
"Daniel T." <po********@earthlink.net> 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.
Mar 11 '06 #4
Micah Cowan wrote:
"Daniel T." <po********@earthlink.net> 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
Mar 11 '06 #5
In article <87************@mcowan.barracudanetworks.com>,
Micah Cowan <mi***@cowan.name> wrote:
"Daniel T." <po********@earthlink.net> 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.
Mar 11 '06 #6
Victor Bazarov wrote:
Micah Cowan wrote:
"Daniel T." <po********@earthlink.net> 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)
Mar 11 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Brent Lievers | last post by:
Greetings, I have a question about parsing a fixed-width integer from a string. Lines of data are being read from a file having a very strict column-delimited format. In my example below,...
12
by: Simone Mehta | last post by:
hi All, I am parsing a CSV file. I want to read every row into a char array of reasonable size and then extract strings from it. <snippet> char foo="hello,world,bye,bye,world"; ........
4
by: smshahriar | last post by:
Hi, I want to scan from the following string all the hex numbers and populate an array of integers: 0x27 0x00 0x30 0x00 0x33 0x00 0x36 0x00
6
by: Rob Thorpe | last post by:
Given the code:- r = sscanf (s, "%lf", x); What is the correct output if the string s is simply "-" ? If "-" is considered the beginning of a number, that has been cut-short then the...
7
by: RSoIsCaIrLiIoA | last post by:
until a poor newbie can build a better function than sscanf and fgets scanf("%s", string) is like gets(string)
8
by: Artemio | last post by:
Dear folks, I need some help with using the sscanf() function. I need to parse a string which has several parameters given in a "A=... B=... C=..." way, and each has a different type (one is a...
20
by: AMP | last post by:
Hello, Anybody know if anything exists like sscanf in c. I found a few things OL but most were pretty old. Maybe something has come along since 2004? Thanks Mike
5
by: Alex Mathieu | last post by:
Hi, using sscanf, I'm trying to retrieve something, but nothing seems to work. Here's the pattern: SS%*sþ0þ%6s Heres the data: SS000000395000000000DC-þ0þ799829þ1174503725þ Actually, I...
5
by: Timo | last post by:
I haven't been using ANSI-C for string parsing for some time, so even this simple task is problematic: I have a string tmp_str, which includes date + time + newline in format: "25.6.2008 21:49"....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.