0

I'm using libconfig. The file .cfg is in the same directory of the .c file. I want to read the .cfg from a specific path. For example i want that the file could be in the previous directory (not in the same directory of the .c file). How can I do it? Here is my code for reading .cfg file (it's standard code):

config_t cfg;
    config_setting_t *setting;

    config_init(&cfg);

    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);

    }
  • I want that the user can specify the path: for example, when he execute file main file ("main"), he can write "./main PATH_OF_THE_CFG_FILE"", where path of the cfg file is the path of the configuration file –  Aug 02 '21 at 10:13
  • 1
    Your question seems to be about very basic C usage. `How can I do it?` You can learn C with [some books](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list0). Here I found some example about parsing command line arguments: https://www.thegeekstuff.com/2013/01/c-argc-argv/ . `I want that the user can specify the path` Great - then do it. With what exactly are you having a problem with? Getting the path from the user? Specifying it for the library? The string `"file.cfg"` looks like the path to configuration file right there. – KamilCuk Aug 02 '21 at 10:19
  • Here https://www.hyperrealm.com/libconfig/libconfig_manual.html#The-C-API I have found libconfig api documentation. – KamilCuk Aug 02 '21 at 10:21
  • See advice on paths https://stackoverflow.com/q/44772007/1216776 – stark Aug 02 '21 at 10:28
  • I know, ho to parse the command line arguments. I don't know ho to retrieve the file from a specific path. –  Aug 02 '21 at 10:31
  • What do you mean with "retrieve the file"? You have a filename and you are calling a function that takes a file name. Which step exactly are you missing? Please show (compilable code) how you handle parameters, what you want to enter and what you want to happen with the parameter. Is your problem how to get from `` + `"file.cfg"` to `"PATH/file.cfg"` ? – Gerhardh Aug 02 '21 at 10:41
  • (&cfg, argv[1]) I solved the problem. Thank you everybody for your help! –  Aug 02 '21 at 10:42

0 Answers0