#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0

    Parsing a config in C


    I want to incorporate a configuration file into some of my programs but I have no idea where to start. I googled it and didn't find much.

    I'm trying to get something like this working:

    (config.cfg)


    name = "chris"
    dl_dir = "C:\chris\"
    age = "15"


    etc.. etc..

    Any links, help or bits of sample code would be appreciated !
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0

    oops


    hehe I meant

    name "chris"
    dl_dir "C:\chris\"
    age "15"


    :)
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    chennai
    Posts
    4
    Rep Power
    0
    after reading the line from the file you can use token for parsing the string

    void main()
    {
    char separator[] = " \"";
    char *token;
    //string is the string that you want to parse

    token = strtok( string, separator );
    while( token != NULL )
    {
    printf( " %s\n", token );
    token = strtok( NULL, seps );
    }
    }

    hope this solves your problem
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    Check out my reply to an earlier post: Parse a config file on linux.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0

    Smile


    Thanks :)
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0
    I looked at the other thread, mitakeet but I've never seen this before:

    std::map < std::string, std::string > myMap;

    or

    std::map < std::string, std::string >::iterator myIterator = myMap.begin();
  12. #7
  13. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    It is C++. You can do the same thing with C but it is very painful. Unless you are stuck in the C world, I suggest you try to make do with the C++ version (the C version would probably be 4 times as long).

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0
    ok I will thanks

IMN logo majestic logo threadwatch logo seochat tools logo