By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,564 Members | 1,081 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,564 IT Pros & Developers. It's quick & easy.

Spaces & Delimiters in a streams

P: n/a
Hi,

I'm having the following problem. I'm trying to write a simple parser
for a script that looks like this:

<command> [<arg-1>], [<arg-2>], [<arg-3>], ...

Giving an argument is optional. If it's not present (meaning there are 2
commans in a row) we take a default value.
Naturally, I'm using an istream to parse each line.

My question is this: is there a way to make a seperation between spaces
and delimiters? The thing is,
that because I have commas between each argument, I want the comma to be
the delimiter, but I still
want to ignore the spaces.

The only two ways I know is either to use getline with ',' as a
delimiter - but that wouldn't ignore spaces.
The other is to write your own facet class which sets ',' as a space. -
but then after reading the first comma,
if there's another comma straight after it - the stream will read on -
taking it as a white space, instead of treating
it as a delimiter and stop there.

Any ideas?

Thanks,
Yaron
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Y. H." <yh*****@netvision.net.il> wrote in message
news:ch**********@news2.netvision.net.il...
Hi,

I'm having the following problem. I'm trying to write a simple parser
for a script that looks like this:

<command> [<arg-1>], [<arg-2>], [<arg-3>], ...

Giving an argument is optional. If it's not present (meaning there are 2 commans in a row) we take a default value.
Naturally, I'm using an istream to parse each line.

My question is this: is there a way to make a seperation between spaces and delimiters?
That depends upon what you're using for delimiters.
The thing is,
that because I have commas between each argument, I want the comma to be the delimiter, but I still
want to ignore the spaces.

The only two ways I know is either to use getline with ',' as a
delimiter - but that wouldn't ignore spaces.
The other is to write your own facet class which sets ',' as a space. - but then after reading the first comma,
if there's another comma straight after it - the stream will read on -
taking it as a white space, instead of treating
it as a delimiter and stop there.

Any ideas?


std::string tok;
std::getline(stream, tok, ',');
std::istringstream iss(tok);
std::string result; /* or whatever type you need */
iss >> result;

This assumes that each 'arg' between the commas has
no embedded whitespace.

And Of course you'll want to add error checking.
-Mike
Jul 22 '05 #2

P: n/a

"Y. H." <yh*****@netvision.net.il> wrote in message
news:ch**********@news2.netvision.net.il...
Hi,

I'm having the following problem. I'm trying to write a simple parser
for a script that looks like this:

<command> [<arg-1>], [<arg-2>], [<arg-3>], ...

Giving an argument is optional. If it's not present (meaning there are 2 commans in a row) we take a default value.
Naturally, I'm using an istream to parse each line.

My question is this: is there a way to make a seperation between spaces and delimiters? The thing is,
that because I have commas between each argument, I want the comma to be the delimiter, but I still
want to ignore the spaces.

The only two ways I know is either to use getline with ',' as a
delimiter - but that wouldn't ignore spaces.
The other is to write your own facet class which sets ',' as a space. - but then after reading the first comma,
if there's another comma straight after it - the stream will read on -
taking it as a white space, instead of treating
it as a delimiter and stop there.

Any ideas?


see www.boost.org. Check out tokenizer, regex and/or spirit.

Jeff F
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.