0

I have the following Config.cfg file and I want to read the values of Device

[BBDD]
user=**
password=***
database=***
IPServidor=*
port=3***

[Device]
dev=8
Temperatura=5=1001
Humedad=7=1002
Link=8=1003
Volt=9=1004

[Device]
dev=10
Temperatura=5=1012
Humedad=7=1013
Link=8=1014
Volt=9=1015

So I try with libconfig library. I wrote the following code but it returns me nothing. Am I missing something? Or is there any problem with libconfig? I tried also with glib but because of the same keyword Device I cannot use it.

#include <stdio.h> // standard input / output functions
#include <string.h> // string function definitions
#include <stdlib.h>
#include <libconfig.h>
#include <glib.h>

int main()
{
  config_t cfg; //returns all parameters in this structure
  config_setting_t *setting;
  const char *str1, *str2;
  int tmp;
  config_init(&cfg);
  if(config_read_file(&cfg, "Config.cfg"))
   {

    fprintf(stderr, "%s=%d\n", config_error_file(&cfg),config_error_line(&cfg),config_error_text(&cfg));
    config_destroy(&cfg);
    return(EXIT_FAILURE);
}
//Read the parameter group
setting = config_lookup(&cfg, "Device");
if (setting!= NULL)
{
    //Read the string
    if(config_setting_lookup_string(setting, "dev", &str1))
    {
        printf("\n Device: &s", str1);
    }
    else
    printf("\n No 'Device' setting in configuration file");
    printf("\n");
}
config_destroy(&cfg);

}

I tried both with Device and [Device] as keyword and also config_setting_lookup_string and config_setting_lookup_int to return the values of dev but nothing happens. My command returns me a simple

Process returned 0. execution time: 0.115sec
timrau
  • 22,578
  • 4
  • 51
  • 64
dali1985
  • 3,263
  • 13
  • 49
  • 68

1 Answers1

0

Your configuration doesn't seem to be a valid libconfig configuration file.

Check the libconfig documentation.

Mr. Beer
  • 255
  • 1
  • 5
  • @dali1985 You are currently trying to parse a property file (ini file). One way to do it is to implement your own parse. . If you prefer the library approach, you can use **Boost::property_tree**. Check this link for more information: [http://stackoverflow.com/questions/6175502/how-to-parse-ini-file-with-boost] – Mr. Beer Jul 08 '13 at 15:58