1

I've an issue:

name = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
// Store inventory:
params =
{
  myparams= ( {  max_len  = 1024UL;
              author = "Robert Louis Stevenson";
              price  = 29.99;
              qty    = 5; }
                );
            };

Do you know how to retrivie from the main this value 1024UL? It's not an integer, but a particular int (unsigned long). I've not found a function to do that. You can see the function config_setting_lookup_int, but obviously it doesn't work becaus the value to retrieve is not an int (1024UL). Thank you everybody for your help!

config_t cfg;
config_setting_t *setting;
const char *str;
config_init(&cfg);

/* Read the file. If there is an error, report it and exit. */
if(! config_read_file(&cfg, "file.cfg"))
{
fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
        config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg);
return(EXIT_FAILURE);
}else{
printf("Config file is OK\n");
}

    /* Get the store name. */
        if(config_lookup_string(&cfg, "name", &str))
        printf("Store name: %s\n\n", str);
        else
        fprintf(stderr, "No 'name' setting in configuration file.\n");
    
    
    
        setting = config_lookup(&cfg, "params.myparams");
      if(setting != NULL)
      {
        int count = config_setting_length(setting);
        int i;
    
        printf("%-30s  %-30s   %-6s  %s\n", "TITLE", "AUTHOR", "PRICE", "QTY");
    
        for(i = 0; i < count; ++i)
        {
          config_setting_t *book = config_setting_get_elem(setting, i);
    
          /* Only output the record if all of the expected fields are present. */
          const char *author;
          int max_len;
          double price;
          int qty;
    
          if(!(config_setting_lookup_int(book, "max_len", &max_len)
               && config_setting_lookup_string(book, "author", &author)
               && config_setting_lookup_float(book, "price", &price)
               && config_setting_lookup_int(book, "qty", &qty)))
            continue;
    
          printf("%-30d  %-30s  $%6.2f  %3d\n", max_len, author, price, qty);
        }
        putchar('\n');
      }
  • 1
    Use `config_setting_lookup_int64`? If libconfig support integer suffixes that is. If it doesn't, then you need to read it as a string and convert it yourself (with e.g. `strtoul`). – Some programmer dude Jul 26 '21 at 10:39
  • You're right. I added the initial part of the code –  Jul 26 '21 at 10:40
  • I'm trying to use config_setting_lookup_int64. how can I declare max_len? I'm trying to write unsigned long max_len; in the file.cfg can i write max_len: 1024UL? ? –  Jul 26 '21 at 10:46

1 Answers1

-1
  • UL suffix meas that the constant integer has type unsigned long
  • ULL - 'unsigned long long'
  • U - unsigned int
  • L - long int`
  • LL - long long int`

It is better to have too long one that is needed, than a too short. In most cases, the compiler will convert it to the correct type.

0___________
  • 60,014
  • 4
  • 34
  • 74