parsing files

E

elcorto

Hi

I'm new to the list and I'm coming with a newbie question:

What would be the best way to parse simple (config) files like

config.txt:

keyA=value1, value2, value3, ...
keyB=value1, value2, value3, ...
....
(the value(i) should be converted to long or doulbe)

Up to now I used getopt (together with strtod() and so on for
converting the optarg) but as the input for my program gets more
complicated getopt
is starting to get painful.

Any hints are appreciated.

cheers,
elcorto
 
B

Bob Hairgrove

Hi

I'm new to the list and I'm coming with a newbie question:

What would be the best way to parse simple (config) files like

config.txt:

keyA=value1, value2, value3, ...
keyB=value1, value2, value3, ...
...
(the value(i) should be converted to long or doulbe)

Up to now I used getopt (together with strtod() and so on for
converting the optarg) but as the input for my program gets more
complicated getopt
is starting to get painful.

Any hints are appreciated.

Try this:
comp.text.xml
 
V

Victor Bazarov

elcorto said:
I'm new to the list and I'm coming with a newbie question:

What would be the best way to parse simple (config) files like

config.txt:

keyA=value1, value2, value3, ...
keyB=value1, value2, value3, ...
...
(the value(i) should be converted to long or doulbe)

Up to now I used getopt (together with strtod() and so on for
converting the optarg) but as the input for my program gets more
complicated getopt
is starting to get painful.

Read a line (using std::geline, for example) and search for the '=' sign,
and then split the line in two at the equal sign. Whatever is to the left
is your name. Whatever is to the right is the list of values. Split the
list of values at the commas and read out the values that way.

Is there a C++ language question here somewhere?

V
 
E

elcorto

Isn't there an (fairly easy) way to do this in C? I thougt about
keeping my getopt mess, read the config file with Python and build the
cmdline options from there but that sound not very elegant to me ...

cheers,
steve
 
E

elcorto

Thanx.

Is this no language question?
Sry if I'm posting to the wrong list. With wich list would I be better
off?

cheers
 
M

mlimber

elcorto said:
Hi

I'm new to the list and I'm coming with a newbie question:

What would be the best way to parse simple (config) files like

config.txt:

keyA=value1, value2, value3, ...
keyB=value1, value2, value3, ...
...
(the value(i) should be converted to long or doulbe)

Up to now I used getopt (together with strtod() and so on for
converting the optarg) but as the input for my program gets more
complicated getopt
is starting to get painful.

Any hints are appreciated.

cheers,
elcorto

You could use an existing and tested parsing library such as

http://boost.org/doc/html/program_options.html

If you'd prefer to craft your own, you could make use of the tokenizer
library:

http://boost.org/libs/tokenizer/index.html

Cheers! --M
 
V

Victor Bazarov

elcorto said:
Is this no language question?

If you're asking what elements of the language to use, then I can tell you
to use 'std::ifstream', 'std::getline' and 'std::string' with all its
members ('find', 'substr', etc.) If you're asking "how to do it", then
you need to create an algorithm first, then translate it into C++. If you
have problems with the translation we can help, but you're the one who
needs to do most of the work.
Sry if I'm posting to the wrong list. With wich list would I be better
off?

I don't think there is one. You need to write a _program_ that does what
you want. I don't see any other way.

You could of course search the web for existing solutions. Visit
www.google.com and search for 'parsing strings'. Keywords like "name
value pairs" might help.

You could of course, use 'lex' and 'yacc'... They are both OT here, so we
can't help you either.

See, you have plenty of choices. Waiting for us to do your job is one of
them, but definitely not the best.

V
 
E

elcorto

Did I say "send me your code"? My question was "what tools does C++
offer me, give me some hints, please".

Well now know that there is a string class which I didn't know before.
Thank you very much for answering my question.

cheers,
elcorto
 
V

Victor Bazarov

elcorto said:
[..]
Well now know that there is a string class which I didn't know before.

Oh, I am sorry. For some strange reason I tend to assume that people
who come here for help at least know the basics of the language, like
the contents of the Standard Library, among other things. What book on
C++ are you reading that doesn't talk about 'std::string'?

V
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top