1

I'm trying to understand how I can create a ".config" file containing a bunch of parameters to later use to set up the variables in my C project on Unix.

I created my ".config" file using sudo nano test.config and wrote some stuff inside such as:

#N is this
N 10
#p is that
p 0.002
#T is this
T 10

Now that I did that how can I read its content and use it to initialize my variables?

Aimery
  • 1,559
  • 1
  • 19
  • 24
Fabzheimer
  • 45
  • 5
  • 1
    Post what you've tried. – Andrew Henle May 16 '18 at 14:28
  • open the file, read the contents, set variables as needed. – Christian Gibbons May 16 '18 at 14:28
  • 1
    Possible duplicate of [File based Configuration handling in C ( Unix )](https://stackoverflow.com/questions/411921/file-based-configuration-handling-in-c-unix) – YesThatIsMyName May 16 '18 at 14:31
  • 1
    @ChristianGibbons just like a regular text file? – Fabzheimer May 16 '18 at 14:37
  • Configuration for which project? Show some [MCVE] please – Basile Starynkevitch May 16 '18 at 14:48
  • 1
    A file is a file is a file — mostly. Yes, a configuration file is just a regular text file designated as a configuration file and formatted in some particular manner. It looks like yours will ignore lines starting with `#` as a comment line, ignore blank lines (for sanity's sake), and expects an alphanumeric keyword and a value. That much is easy. Lots of details left to worry about — are spaces significant in the value part; can you quote strings or spaces, what names are important, are there sections in the file. – Jonathan Leffler May 16 '18 at 15:01

1 Answers1

0

The several answers to this question explain how to parse that config file, but you could use standard parsing techniques (perhaps your own recursive descent parser) or Glib's lexical scanning or key-value pair parser (or use something else). You certainly should define and document (perhaps using EBNF) what is the format of that textual configuration file (and what the various entries there represent: for example, if that configuration file refers to other files, how do you handle spaces in such file paths, etc....). A common (but not universal) convention is to consider as comments so skip any line starting with #.

Another question is how to get that config file while running in an arbitrary working directory. You just need to build the absolute path of your file (for fopen(3) or open(2)), e.g. with

char configpath[100];
snprintf(configpath, sizeof(configpath), "%s/.test.config", getenv("HOME"));

You could test before that getenv("HOME") is not NULL, but in practice that is very unlikely; see environ(7) and getenv(3); and the case when it gives a very long file path is also unlikely; you might test that snprintf(3) returns a count less than sizeof(configpath) or use asprintf(3).

You might use other functions, e.g. glob(3) or wordexp(3) to get that file path (but you probably should stick to snprintf or asprintf with getenv("HOME")...).

You might consider instead embedding some scriptable interpreter like lua or guile in your program (but that is a strong architectural design decision). Then the configuration file becomes some (Turing-complete!) script.

BTW, there is no need to use sudo to edit that configuration file (under your home directory), and you might decide to also read some system-wide configuration under /etc/

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547