parsing files

Discussion in 'C++' started by elcorto, Jan 17, 2006.

  1. elcorto

    elcorto Guest

    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
    elcorto, Jan 17, 2006
    #1
    1. Advertising

  2. On 17 Jan 2006 12:41:29 -0800, "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.


    Try this:
    comp.text.xml

    --
    Bob Hairgrove
    Bob Hairgrove, Jan 17, 2006
    #2
    1. Advertising

  3. 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
    Victor Bazarov, Jan 17, 2006
    #3
  4. elcorto

    elcorto Guest

    I'm not planning to use XML.

    cheers,
    elcorto
    elcorto, Jan 17, 2006
    #4
  5. elcorto

    elcorto Guest

    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
    elcorto, Jan 17, 2006
    #5
  6. elcorto

    elcorto Guest

    Thanx.

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

    cheers
    elcorto, Jan 17, 2006
    #6
  7. elcorto

    mlimber Guest

    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
    mlimber, Jan 17, 2006
    #7
  8. elcorto

    elcorto Guest

    Ah, thx. I was looking for something like that.

    cheers,
    elcorto
    elcorto, Jan 17, 2006
    #8
  9. 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
    Victor Bazarov, Jan 17, 2006
    #9
  10. elcorto

    elcorto Guest

    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
    elcorto, Jan 17, 2006
    #10
  11. elcorto wrote:
    > [..]
    > 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
    Victor Bazarov, Jan 17, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GIMME
    Replies:
    2
    Views:
    873
    GIMME
    Feb 11, 2004
  2. Naren
    Replies:
    0
    Views:
    579
    Naren
    May 11, 2004
  3. Christopher Diggins
    Replies:
    0
    Views:
    610
    Christopher Diggins
    Jul 9, 2007
  4. Christopher Diggins
    Replies:
    0
    Views:
    433
    Christopher Diggins
    Jul 9, 2007
  5. John Levine
    Replies:
    0
    Views:
    729
    John Levine
    Feb 2, 2012
Loading...

Share This Page