Search code examples
clinuxconfiguration-files

Is there a standard spreaded way to parse config files


I want to create a configuration file for my program in C.

I know how I can do this and the problem is I have several ideas in my mind.

I was researching but I cannot find any generalized standard way to create and parse config files, so I am afraid that I will just create a file with some kind of structure like this:

# Section1
STRING_parameter1          Value1
STRING_parameter2          Value2

# Section2
STRING_other_parameter1    Value3
STRING_other_parameter2    Value4

        ...

        etc

And then just parse the file from the different modules, every section in a normal way.

But I am afraid I can come out with some long term problems that only experience can teach and then to have to modify this structure.

That is why I would like to know if there is a better way to do this in order to avoid future issues.


Solution

  • Although you may pair the C language with other scripting and parsing languages, like xml, json, yaml, there is a simple standard way that is used by those who want to have C only programming environment. Matter of taste, I guess. You may simply have an ASCII .txt file with predetermined format, read it from C and update it either manually or by some other problem. This is the simplest solution that will be always C compatible. However, if you want to go json, or yaml, go ahead, by all means. Just keep in mind, that maintenance may be a bit complicated, if your code gets into the hands of someone who does know C, and doesn't know jsom, yaml or xml. I hope that this answer your question.