Thread: Parsing a INI file

  1. #1
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094

    Parsing a INI file

    I am attempting to parse a ini file but I can't seem to find the commands I can use.


    Code:
    section main { //the start
    
    /////////////////////////////////////////////
    // MAP SETTINGS
    //
    /////////////////////////////////////////////
    
    	ShowThingsOnMap="TRUE"; //show stuff 
    	ThingsAttributes="TRUE"; //prints stuff
    	FlyingThings="TRUE"; //prints small squares for all missiles
    	ShowBigThingsNames="TRUE"; //prints boss stats
    } //the end
    That is a sample part of the INI (with values/variables changed). My problem is finding the right functions and how to pass the right variables through them. I searched the net, and couldn't find anything on parsing text files, just writing to them and reading from them. And how to move the pointer in the file ahead a certain number of places.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    win32 api has a few functions that maintain .ini files, but if you can't use those, then you will have to write your own program.

    All .ini files I have seen are in this form
    Code:
    [ header1-tag ]
       	ShowThingsOnMap="TRUE"; //show stuff 
    	ThingsAttributes="TRUE"; //prints stuff
    	FlyingThings="TRUE"; //prints small squares for all missiles
    	ShowBigThingsNames="TRUE"; //prints boss stats
    
    [ header2-tag ]
     	ShowThingsOnMap="TRUE"; //show stuff 
    	ThingsAttributes="TRUE"; //prints stuff
    	FlyingThings="TRUE"; //prints small squares for all missiles
    	ShowBigThingsNames="TRUE"; //prints boss stats
    [ header3-tag]
    Your program needs to first find the header-tag text. After that is found the progam can parse each of the "value=" lines. use getline() to read each line into a std::string object, then use normal std::string functions to search for the text you want.
    Code:
    #include <fstream>
    #include <string>
    
    int main()
    {
      std:: fstream in("filename");
      std::string line;
    
      while( getline(in,line))
      {
        // put your code here
    
      }
    Last edited by Ancient Dragon; 10-16-2005 at 04:30 AM.

  3. #3
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    So I would just gather up the whole line, then use string functions to search? That seems so simple, I don't know why I didn't think of that.

  4. #4
    Code Ripper
    Join Date
    Jun 2004
    Posts
    30
    Wraithan,

    take a look in GetPrivateProfileString() API... it parse .ini file pretty similar to your ones

    jmgk

  5. #5
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Thanks for the help so far!

    Ok, I found a way for me to parse the variable from the value, and display them. The problem is when I use 2 ints as the arguements for .substr the second one not used... Also, my next step is going to be to make it stop displaying the lines of //////////////// so any tips on that are welcome.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    int main ( int argc , char ** argv )
    {
    	std::string filename = "C:\\Program Files\\EasyPlayer\\easyplay.ini";
    	if (argc >= 2) filename = argv[1];
    	std::ifstream file( filename.c_str() , std::ios::in );
    	std::cout << "Reading from: " << filename << std::endl;
    	std::string line;
    	while (getline( file , line ))
    	{
    		if (line.empty() || (line == "\r")) continue;
    		int equal_pos = line.find('=');
                    int comment_pos = line.find('/' && '/');
    		std::string key = line.substr(0,equal_pos);
    		std::string value = line.substr(equal_pos+1,comment_pos);
    	        std::string comment = line.substr(comment_pos+2,line.size()-comment_pos-1);
    
    		std::cout << "Key: " << key << "    " << "Value: " << value << "    "  << "Comments: " << comment << std::endl;
    	}
    	std::cin.get();
    }
    ~Wraith

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Nvm, changing my stratigy, going to parse one character at a time, seems like it would be easier.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    All c++ line comments begin with double slash, not single slash. Stripping comments should be done first, before any other checks. If, after that, the line is empty then just continue reading the next line.

    And parsing the file one character at a time will not make it any easier.
    Code:
    int comment_pos = line.find("//");
    if(comment_pos >= 0)
       line = line.substr(0,comment_pos);
    
    // do the rest of the checking here
    Last edited by Ancient Dragon; 10-17-2005 at 05:20 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  3. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM