question about using istringstream

Started by
3 comments, last by demonkoryu 19 years, 8 months ago
i am trying to parse a text file with the following format: integer:"string that might have spaces":"another string that might have spaces" is there a way that i can set the delimiters for the istringstream so it reads until it hits a : that is NOT in quotes?
Advertisement
Not trivially, no. May I suggest using something like boost::tokenizer ?
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
If you're feeling brave about Regular Expressions, you might be able to use boost::regex to achieve the task. Failing that, write a grammar using boost::spirit and use that to parse it.

As Fruny said, it's not the most trivial of tasks. You could write your own simple parser by reading the characters and using state machines if yu feel like a mini-challenge.
thanks for the replys

i decided to end up using a regular expression library that i am pretty familiar with, just wanted to see if there was any easy way to use an istringstream with delimiters i set before i used the regex class
Although you already have a solution, why not just take MSDN's advice on this topic:
Quote:
In such a case, one alternative is to use the unformatted input member function istream::getline to read a block of text with white space included, then parse the block with special functions. Another method is to derive an input stream class with a member function such as GetNextToken, which can call istream members to extract and format character data.

The parsing algorithm sure is simple. If you don't use Regex elsewhere it may be worth removing the dependency on it because of compilation speed and size concerns.

Thermo

This topic is closed to new replies.

Advertisement