Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 8th, 2005, 4:43 PM   #1
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 309
Rep Power: 4 Klarre is on a distinguished road
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 is offline   Reply With Quote
Old Aug 8th, 2005, 5:15 PM   #2
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 309
Rep Power: 4 Klarre is on a distinguished road
Ok, I solved it after a chat with a friend. I changed the char* tmp to a char tmp[32].
Klarre is offline   Reply With Quote
Old Aug 8th, 2005, 6:30 PM   #3
prolog
Programmer
 
Join Date: Jul 2005
Location: Germany
Posts: 69
Rep Power: 4 prolog is on a distinguished road
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
prolog is offline   Reply With Quote
Old Aug 8th, 2005, 6:35 PM   #4
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 309
Rep Power: 4 Klarre is on a distinguished road
Ah, thanks!

Is there a way to solve it? Use of malloc(), or something like that?
Klarre is offline   Reply With Quote
Old Aug 8th, 2005, 6:38 PM   #5
prolog
Programmer
 
Join Date: Jul 2005
Location: Germany
Posts: 69
Rep Power: 4 prolog is on a distinguished road
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
prolog is offline   Reply With Quote
Old Aug 8th, 2005, 6:42 PM   #6
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 309
Rep Power: 4 Klarre is on a distinguished road
Yes, it helped!

Thanks alot!
Klarre is offline   Reply With Quote
Old Aug 8th, 2005, 7:16 PM   #7
Animatronic
Programmer
 
Join Date: Jun 2005
Posts: 99
Rep Power: 4 Animatronic is on a distinguished road
Unless this is just for learning, there are win32 functions for reading in an .ini file. Look up GetPrivateProfileString(), and its close related functions.
Animatronic is offline   Reply With Quote
Old Aug 9th, 2005, 4:21 AM   #8
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 309
Rep Power: 4 Klarre is on a distinguished road
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!
Klarre is offline   Reply With Quote
Old Aug 9th, 2005, 4:24 AM   #9
prolog
Programmer
 
Join Date: Jul 2005
Location: Germany
Posts: 69
Rep Power: 4 prolog is on a distinguished road
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
prolog is offline   Reply With Quote
Old Aug 9th, 2005, 9:08 AM   #10
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:40 AM.

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