Open CFG File

Information, tips and instructions

Using CFG Files in Code

In this manual we will go into details about how CFG files work and how they could be formatted. We will only review plain text based CFG files because binary configuration files do not have specific format defined. Also, binary files could be viewed and edited outside the software which created them only in rare circumstances.

A configuration file can be as simple as defining a variable, for example.

Config file test.cfg

USERNAME = David

In the configuration file above the configuration variable (USERNAME) is assigned with the “Test” value. Now, let’s write a small script file that uses the previously defined variable in test.cfg file:

Script file test.sh

#!/bin/bash
source test.cfg
echo "Hello, $USERNAME "

In the first line, the operating system is told how to handle this file through the “bash” shell scripting program. Then the script reads the CFG file, takes the defined USERNAME value from there, and prints the word 'Hello' on the 'echo' line, followed by the value of the variable defined in the CFG file.

Now let’s review how compiled applications could use CFG files. All application binary files compile from a source code written by a programmer. In that way they are quite similar to scripts. The difference is that the source code that generate binaries must be translated (compiled) beforehand to create the binary. Once the binary is created, it cannot be read by humans, only by machines.

An example of a source of a binary file written in C++ language is as follows:

C source file hello.c

#include
#include

int main (void)
{
  FILE * file = fopen ("hello.cfg", "r");
  if (file) {
    char line [1024]; // Definition of variable line
    char username [256]; // Definition of variable username
    while (fgets (line, sizeof (line), file)) { // Reading of number of lines and characters in cfg
      if (sscanf (line, "USERNAME =% s", username) == 1) { // Setting of value of the variable
        printf ("Hello, %s \ n", username); // Printing the value
        break;
      }
    }
    fclose (file);
  }
  return 0;
}

As you can see, the source file is a bit more complex than the script described above. It should be noted that the machine does not read this code by itself, it must be previously compiled so that the machine can read it. Once compiled, the generated file is binary and therefore can no longer be read (at least not so easy) by humans.

The advantage of binary files is that they run much faster than scripts since in the case of binaries the syntax was checked at the time of compilation only once. In scripts, this check is made every time they are ran and this translates into a consumption of computation time.

On the other hand, as you could see above, the scripts are easily readable by humans, and therefore are easily editable.

In conclusion, it makes not much sense to make CFG files binary, because they must be readable and editable by humans. However, it is possible for a programmer to treat some configuration files as binary, to avoid infection with viruses or accidental modification by application users. In this case, the programmer must make sure to create tools that facilitate the editing of the CFG file.