Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Need to update some parametr in the config file using LIBConfig C API (http://www.go4expert.com/forums/update-parametr-config-file-using-t28027/)

madhugupta 15Mar2012 18:06

Need to update some parametr in the config file using LIBConfig C API
 
Hi,
I am new to programming.My requirement is to update the parameter value in my config file using Libconfig C API.
This is the .cfg file.
example.cfg
/ An example configuration file that stores information about a store.

Code:

// Basic store information:
name = "Books, Movies & More";

// Store inventory:
inventory =
{
  books = ( { title  = "Treasure Island";
              author = "Robert Louis Stevenson";
              price  = 29.99;
              qty    = 5; },
            { title  = "Snow Crash";
              author = "Neal Stephenson";
              price  = 9.99;
              qty    = 8; }
          );

  movies = ( { title = "Brazil";
              media = "DVD";
              price = 19.99;
              qty = 11; },
            { title = "The City of Lost Children";
              media = "DVD";
              price = 18.99;
              qty = 5; },
            { title = "Memento";
              media = "Blu-Ray";
              price = 24.99;
              qty = 20;
            },
            { title = "Howard the Duck"; }
          );
};

// Store hours:
hours =
{
  mon = { open =  9; close = 18; };
  tue = { open =  9; close = 18; };
  wed = { open =  9; close = 18; };
  thu = { open =  9; close = 18; };
  fri = { open =  9; close = 20; };
  sat = { open =  9; close = 20; };
  sun = { open = 11; close = 16; };
};


the corresponding code is
--------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <libconfig.h>

/* This example reads the configuration file 'example.cfg', adds a new
 * movie record to the movies list, and writes the updated configuration to
 * 'updated.cfg'.
 */

int main(int argc, char **argv)
{
  static const char *output_file = "example.cfg";
  config_t cfg;
  config_setting_t *root, *setting, *movie;

  config_init(&cfg);

  /* Read the file. If there is an error, report it and exit. */
  if(! config_read_file(&cfg, "example.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);
  }
//
  /*Need the function to update the value of
title  = "algorithm design"; instead of title  = "Treasure Island";
wed = { open =  10; close = 19; }; instead of wed = { open =  9; close = 18; };

*/

  /* Write out the updated configuration. */
  if(! config_write_file(&cfg, output_file))
  {
    fprintf(stderr, "Error while writing file.\n");
    config_destroy(&cfg);
    return(EXIT_FAILURE);
  }

  fprintf(stderr, "Updated configuration successfully written to: %s\n",
          output_file);

  config_destroy(&cfg);
  return(EXIT_SUCCESS);
}

/* eof */



All times are GMT +5.5. The time now is 05:04.