Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   INI-file reader (http://www.programmingforums.org/showthread.php?t=5323)

Klarre Aug 8th, 2005 3:43 PM

INI-file reader
 
I am stucked with a very frustration problem. The program crashes on the getline(...) row. Any ideas why?

:

#include <iostream>
#include <fstream>

int settingInt(const char* fileName, const char* caption, const char* setting)
{
        std::ifstream file;
        file.open(fileName);
        if(!file.is_open())
                return -2;

        char* tmp;
        while(!file.eof())
        {
                if(file.get() == '[')
                {
                        file.getline(tmp, 32, ']'); // CRASH!
                        // Continue implement reader here...
                }
        }

        file.close();
        return -1;
}

int main()
{
        std::cout << "Setting: " << settingInt("thefile.ini", "physics", "gravity") << std::endl;

        return 0;
}


The INI file look like this:

:

[general]
resolutionx=800
resolutiony=600

[physics]
enable=1
gravity=9.82

[keymap]
jump=space
walkforward=w
walkbackward=s
strafeleft=a
straferight=d


Klarre Aug 8th, 2005 4:15 PM

Ok, I solved it after a chat with a friend. I changed the char* tmp to a char tmp[32].

prolog Aug 8th, 2005 5:30 PM

Hi,

do you know why it crashed ? if not i'll tell you. You did not allocate space for your buffer, so getline tried to write date into memory which did not belong to you. That is where the "big bang" kicked in.

Klarre Aug 8th, 2005 5:35 PM

Ah, thanks!

Is there a way to solve it? Use of malloc(), or something like that?

prolog Aug 8th, 2005 5:38 PM

Hi,

yes there is a way. As you are coding in c++ new is the operator you'll want to use.

just allocate buffer this way:
:

char *tmp=new char[32] ;//or whaterver size

//some code

delete [] tmp; //release the buffer


A better way to do things where exception conditions are likly to accure, is to use smart-pointers which exploit RAII to assure that any resources previously acquired are released in all circumstances.

Hope it helped ...

So far ...

Klarre Aug 8th, 2005 5:42 PM

Yes, it helped!

Thanks alot!

Animatronic Aug 8th, 2005 6:16 PM

Unless this is just for learning, there are win32 functions for reading in an .ini file. Look up GetPrivateProfileString(), and its close related functions.

Klarre Aug 9th, 2005 3:21 AM

Yes, it is just for learning. Actually I read about that function, and thought that "I do it myself instead"! I am also coding for BeOS, so I want portable code.

Thanks anyway!

prolog Aug 9th, 2005 3:24 AM

Hi Klarre,

if you want something more powerful you could use boost:: program_options for *nixes or boost::serialization for win32 and *nixes.

Infinite Recursion Aug 9th, 2005 8:08 AM

I would normally use the 'string' data type in this instance, instead of char*


All times are GMT -5. The time now is 4:24 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC