How to parse a .config file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brian behnia
    New Member
    • Jul 2010
    • 1

    #1

    How to parse a .config file

    Hello all,

    I have a summer internship and am creating a GUI in QT to generate a configuration file for a very complicated blind deconvolution process. My job is rather easy, though.

    I have created the GUI and made an event for saving the parameters in to a .txt config file. Now, though, I need to be able to load a configuration file and set all the fields and checkboxes based on the values in the file.

    I need to parse it in in C++. I haven't programmed c++ in a long time.

    The file looks like:

    ############### ############### ######
    # COMMENTS
    ############### ############### ######
    VARIABLE: VALUE

    ############### ############### ######
    # COMMENTS 2
    ############### ############### ######
    VARIABLE2: VALUE2

    These values can be stored as chars by my program.

    I basically need to store each line in a string or cstring, throw out lines with a '#' in the front, and then split the "VARIABLE:VALUE " in to two separate strings using the : as a delimiter.

    Once I have the two strings, I was just thinking of doing a bunch of if statements to match the string 1 VARIABLE to the var in my gui (named the same thing) and then assign the value with string2.

    I could really use some help. I'm going to call my programming buddies tonight but I should have had this done already.

    Much obliged,
    Bri
    Last edited by Niheel; Feb 7 '13, 06:48 PM. Reason: noobish
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    I would do something like use getline ro read lines from files. Put those lines into a stringstream and use that to read out (using getline again) parameters 1 at a time delimited by :. If the first character of the first parameter is a # then its a comment otherwise the first parameter read is the variable name and the second parameter read is its value.

    This assumes none of the values or variable names contain :

    Comment

    Working...