0

I need to be able to modify a complex config file from within my C/C++ program (whichever is going to be easier and more convenient to use in this case, probably C++). The idea is that the program computes the values that it needs to insert into the file, then writes them. The config file itself looks like this:

^looots of stuff I won't be using^
option blah.blah.something "value"
option blah.blah.someotherthing "value"


Now, many of the options are logically connected, for instance:

option blah.blah.car.engine "value"
option blah.blah.car.color "value"

I don't really have an idea how to reach those lines that I'm interested in. Should I skip to a specific line and then search for a quotation mark and modify what's after it? That doesn't seems like a reliable and flexible solution, does it?

user4520
  • 3,401
  • 1
  • 27
  • 50
  • 8
    If you are not just appending to the file then modifying it can be tricky. I would just write the entire file as a new file each time. – james82345 Sep 14 '13 at 14:05
  • How often do you need to modify the config file ? Generally, configuration files are modified not very often, atleast by the program that reads it. The user sets options according to his or her needs and the program that depends on this config file reads it when it starts and uses the config options. If the user or any other program changes the config file when the program is running, it does not affect it. – Vivek S Sep 14 '13 at 14:23
  • This is a pretty rare case; my program modifies a config file for a much bigger and complex program, which reloads it every 10-15 minutes. – user4520 Sep 14 '13 at 14:33
  • 1
    If your config file can be any shape you would like, then two options come to mind, both are human and program readable and editable. .ini and .xml. .ini has been deprecated but is still much in use today as a simple initialization file (or config file). And both have libraries to assist in reading and editing a specific config item within the file. – ryyker Sep 14 '13 at 15:32
  • 1
    What is the complete syntax of the config file? – Thomas Matthews Sep 14 '13 at 17:51
  • There is really absolutely no way we can guide you in how to parse a file unless we know the complete format. As a trivial example, if your config file supports python style multiline quotes with """, it is entirely possible that a line of text content would look like `option bleble.bleble.car.engine "value"` and break a simple script that looks for that. But, on the flip side, if you know these things cannot and will not occur, you you have access to parsing tricks like "scan for a line that starts with" which are far faster to implement than a full parser for the config file. – Cort Ammon Sep 14 '13 at 18:15
  • The config fle format is forced by the application that uses it, however, as @JamesEldridge suggested, I was able to solve the problem using the "append" approach. Thank you all! – user4520 Sep 14 '13 at 19:05
  • Do all lines have the format `option ""`? – Zane Sep 14 '13 at 19:15
  • http://stackoverflow.com/questions/6892754/creating-a-simple-configuration-file-and-parser-in-c – Mike Makuch Sep 15 '13 at 00:43

0 Answers0