Thread: Question about file I/O for configuration files.

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    9

    Question about file I/O for configuration files.

    I'm kind of new to C, and I was wondering if someone could point me in the direction of how to correctly read the following format:


    BotNick "exampletest";
    BotUser "exampleUser";

    Just give me some general pointers on correctly reading that so that the "exampletest" and such can be set to variables to be used by my programme. Thanks for your help!

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I don't understand the question. You want to be able to read input from the user?

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    9
    No, I'm making a bot that connects to IRC. I want the bot to have a configuration files so that values like the bot's nick can be easily changed.

    for instance, there's a file "bot.conf' that contains

    BotNick "ReallyCoolBot";
    BotUser "Stephen";

    Those should be read from the file and set to variables that my program can use, this makes the bot configurable.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    So if you've gotten that far in making an IRC bot, you should be able to read data from a file.... So read it (using fgets()), and depending on the first string (ie. BotNick), set it to the value of the second string (ie. "ReallyCoolBot").

    Where's the part that you're confused about?

  5. #5
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    God, MSDN is so stupid even google knows MSDN better than MSDN knows MSDN...
    Anyways, I think I've got the right function: GetPrivateProfileString (in the registry section NOT the File Management Function section... go figure Microsoft geniuses). Check that out and its family of functions if your using Windows to easily read/write variables to/from a .ini file.
    Oddly enough, the function seems to be marked depricated, and they advise you to use registry functions instead. Whether its the function itself that is depricated and there is a better one to use for reading/writing .ini files, or whether Microsoft is truly advocating always using the registry for startup info rather than .ini files (which again is utterly stupid, allowing sloppy developers to clutter up the users' registries, or even forcing good ones to maintain some formal uninstallation routine, even for small apps where simply deleting the folder would be easier).
    But yes, let me express my curiousity at the fact that you've managed to create an IRC bot without any knowledge of C or basic file I/O ...

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    char *getTagvalue(char *TagName, char *filename)
    {
         FILE *fp;
         char buff[BUFSIZ];
         char Tagn[50];
         char Tagv[50];
         
         if((fp = fopen(filename, "r")) == NULL)
                return NULL:
         else
         {
             while(fgets(buff, sizeof buff, fp) != NULL)
             {
                 sscanf(buff, "%s %s", Tagn, Tagv);
                 
                 if(strcmp(Tagn, TagName) == 0)
                 {
                    fclose(fp);
                    return Tagv;
                 }
             }
         }
         fclose(fp);
         return NULL;
    }
    This function would return you back the Tagvalue with you send the Tagname name and the filename (config file name).

    ssharish2005

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    9
    it's not that i dont know HOW to do it, it's that i always feel that my method is faulty. like.. the way i have it now, i read with fgets and use strtok to tokenize the value and set it to a variable. I was basically just wondering if that's an efficient way, or if there's a better one.

  8. #8
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Well if your entire config file is of the form:
    "Variable"="Setting"
    and your designing for a Windows platform, then using the WinAPI I mentioned would probably be most effecient in terms of programming efforts (though I'm not certain the internals of that particular API, I'm sure it is probably sufficiently effecient).
    If on the other hand you have your own custom, varying layout for your config file or want to be able to change its layout later on, then as the other guys suggested you might want to implement your own file reading function using fgets and parsing in the order you expect items.

    Another implementation to consider is using an XML layout, but usually this is more beneficial for user-reading/editing and/or "formal" purposes, and can largely turn out to be a pain in the ass to implement in C.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    fgets() + strtok() seems a reasonable approach for small config files with limited variation in internal syntax.

    XML would work as well, but use a library (say expat) to keep your sanity.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    A question related to this topic. What happens in Unix configuration files. They are not XML file. But still the config file are quite long. How do they parse those file. Do they still use same tech such as fgets+strtok function. Or is there any other methods to make work faster?

    ssharish

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Since config files are typically read just once at startup, I would think people would prefer functionality over raw speed. fgets() is about the quickest way of reading a text file without going horribly non-portable.

    I'm not aware of any particular library which deals with config files in a good way, like say there is getopts() for dealing with command line flags.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM