![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 309
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#2 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 309
Rep Power: 4
![]() |
Ok, I solved it after a chat with a friend. I changed the char* tmp to a char tmp[32].
|
|
|
|
|
|
#3 |
|
Programmer
Join Date: Jul 2005
Location: Germany
Posts: 69
Rep Power: 4
![]() |
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.
__________________
-= C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do succeed, you will blow away your whole leg. =- Bjarne Stroustrup |
|
|
|
|
|
#4 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 309
Rep Power: 4
![]() |
Ah, thanks!
Is there a way to solve it? Use of malloc(), or something like that? |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Jul 2005
Location: Germany
Posts: 69
Rep Power: 4
![]() |
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 ...
__________________
-= C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do succeed, you will blow away your whole leg. =- Bjarne Stroustrup |
|
|
|
|
|
#6 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 309
Rep Power: 4
![]() |
Yes, it helped!
Thanks alot! |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Jun 2005
Posts: 99
Rep Power: 4
![]() |
Unless this is just for learning, there are win32 functions for reading in an .ini file. Look up GetPrivateProfileString(), and its close related functions.
|
|
|
|
|
|
#8 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 309
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Jul 2005
Location: Germany
Posts: 69
Rep Power: 4
![]() |
Hi Klarre,
if you want something more powerful you could use boost:: program_options for *nixes or boost::serialization for win32 and *nixes.
__________________
-= C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do succeed, you will blow away your whole leg. =- Bjarne Stroustrup |
|
|
|
|
|
#10 |
|
Programming Guru
![]() ![]() ![]() |
I would normally use the 'string' data type in this instance, instead of char*
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|