Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > parsing files

Reply
Thread Tools

parsing files

 
 
elcorto
Guest
Posts: n/a
 
      01-17-2006
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

 
Reply With Quote
 
 
 
 
Bob Hairgrove
Guest
Posts: n/a
 
      01-17-2006
On 17 Jan 2006 12:41:29 -0800, "elcorto" <(E-Mail Removed)> wrote:

>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

--
Bob Hairgrove
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-17-2006
elcorto wrote:
> 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
 
Reply With Quote
 
elcorto
Guest
Posts: n/a
 
      01-17-2006
I'm not planning to use XML.

cheers,
elcorto

 
Reply With Quote
 
elcorto
Guest
Posts: n/a
 
      01-17-2006
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

 
Reply With Quote
 
elcorto
Guest
Posts: n/a
 
      01-17-2006
Thanx.

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

cheers

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      01-17-2006
elcorto wrote:
> 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

 
Reply With Quote
 
elcorto
Guest
Posts: n/a
 
      01-17-2006
Ah, thx. I was looking for something like that.

cheers,
elcorto

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      01-17-2006
elcorto wrote:
> 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
 
Reply With Quote
 
elcorto
Guest
Posts: n/a
 
      01-17-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What libraries should I use for MIME parsing, XML parsing, and MySQL ? John Levine Ruby 0 02-02-2012 11:15 PM
[ANN] Parsing Tutorial and YARD 1.0: A C++ Parsing Framework Christopher Diggins C++ 0 07-09-2007 09:01 PM
[ANN] Parsing Tutorial and YARD 1.0: A C++ Parsing Framework Christopher Diggins C++ 0 07-09-2007 08:58 PM
SAX Parsing - Weird results when parsing content between tags. Naren XML 0 05-11-2004 07:25 PM
Perl expression for parsing CSV (ignoring parsing commas when in double quotes) GIMME Perl 2 02-11-2004 05:40 PM



Advertisments