473,386 Members | 1,886 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,386 software developers and data experts.

Spaces & Delimiters in a streams

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
2 2218

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Francesco | last post by:
I've created a form asking a user to enter a text; the user can press key or any other "strange" non printable char that i want to strip away from the entred text and replace it with a " "...
4
by: shea martin | last post by:
Is there a way to change the delimiters used by >> in an istream? I am parsing a markup file, and want <tag>word</tag> to be broken into 3 strings. I would prefer not to use a 3rd party lib,...
3
by: whatduck | last post by:
I'm having trouble passing a variable that contains spaces. If the variable contains a space I get the following error: "Application uses a value of the wrong type for the current operation." ...
12
by: Brian Henry | last post by:
first question... I have a flat file which unfortinuatly has columns seperated by nulls instead of spaces (a higher up company created it this way for us) is there anyway to do a readline with this...
8
by: Viswanathan S | last post by:
Hi All! What is the major difference or advantage of using VB.NET to create application compare to Visual Basic 6.0? Thanks in Advance S. Viswanathan
46
by: Sensei | last post by:
I was having an interesting discussion about the ANSI C and some ``weird inconsistencies'', or at least what at first sight can be seen as an imbalance. I hope someone can satisfy my curiosity. ...
30
by: Alf P. Steinbach | last post by:
I once suggested in that SomeOne Else(TM) should propose a string value class that accepted literals and char pointers and so on, with possible custom deleter, and in case of literal strings just...
11
by: ramu | last post by:
Hi, Suppose I have a string like this: "I have a string \"and a inner string\\\" I want to remove space in this string but not in the inner string" In the above string I have to remove...
1
by: Venkat143 | last post by:
Hi, I need some help with vb scripting. While setting the property of delimiters to start reading from a space of tab, i used the following code to read then write: If Variables Then ...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.