problem with string and fstream classes

P

Pawel_Iks

Hello!

I would like to write my own class which read text files with some
special form and get some data from this file. Here is the example of
typical txt file which i'm going to use:

dimensions:
a = 3, // description of a
b=10, //description of b
c=23;

variables:
float sth(a,b); //description
sth:prop1="description"
float sth_else(a,b); //description
sth:prop1="description"

data:
a={0,1,2,3,4};
b={4,5,3,2}
c={1,1,1,1,1,1,1,2,3,4,5,56,6,4,3,3,54,3};

now I would like to use string and fstream classes to read such file
and get dimension names, dimension values etc. I need to neglect
strings starting with '//' and so on. Maybe there is some other class
to deal with this problem. I will be grateful for all kinds of hints...
 
V

Vaclav Haisman

Pawel_Iks wrote, On 15.7.2009 12:07:
Hello!

I would like to write my own class which read text files with some
special form and get some data from this file. Here is the example of
typical txt file which i'm going to use:

dimensions:
a = 3, // description of a
b=10, //description of b
c=23;

variables:
float sth(a,b); //description
sth:prop1="description"
float sth_else(a,b); //description
sth:prop1="description"

data:
a={0,1,2,3,4};
b={4,5,3,2}
c={1,1,1,1,1,1,1,2,3,4,5,56,6,4,3,3,54,3};

now I would like to use string and fstream classes to read such file
and get dimension names, dimension values etc. I need to neglect
strings starting with '//' and so on. Maybe there is some other class
to deal with this problem. I will be grateful for all kinds of hints...
What you need is a parser and maybe a lexer, too. Your special file syntax
seems simple enough that you could do with just a hand made recursive descent
parser. There is not much that the standard facilities can help you with. I
would probably just read the whole file into a std::string and then use
peek_char/get_char pair of operations to implement such recursive descent
lexer and parser.

Alternativelly, you might want to take a look at something like Flex/Bison or
ANTLR or even Boost.Spirit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top