Jump to content
  • Advertisement
Sign in to follow this  
sheep19

[C++] problem reading settings from text file

This topic is 3328 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I'm making a Settings class. It opens "settings.txt". constructor
Settings::Settings()
: _in("settings.txt"), _fullScreen(false)
{
	// handle error in opening file
	if( !_in )
	{
		_in.close();
		std::ofstream out("settings.txt");
		out << "fullscreen=true";
		out.close();
		_in.open("settings.txt");
	}

	string temp;
	while( getline(_in, temp) ) // add text lines in the vector
		_vec.push_back(temp);

	_in.close();

	exit(_vec.size());

	RemoveSpaces();
	SetFullScreen();
}

The constructor opens "Settings.txt". If the file does not exist, it is created. However, something goes wrong. If it doesn't exist, a runtime error appears. When I stop the program from running, the file is created and is fine the next time I run it. However I want it to create the file properly so that it creates it and runs on the first time. What I'm I doing wrong? Isn't that correct?
// handle error in opening file
	if( !_in )
	{
		_in.close();
		std::ofstream out("settings.txt");
		out << "fullscreen=true";
		out.close();
		_in.open("settings.txt");
	}

Share this post


Link to post
Share on other sites
Advertisement
Sorry, I didn't post the exact code:

The error is Microsoft Visual Studio C Runtime Library has detected a fatal error in The Myth of 2012.exe.

Press Break to debug the program or Continue to terminate the program.


:-/

It's in SetFullScreen().

Btw in the constructor, I added this line for testing:
exit(_vec.size());

When the file exists, it exits with 1. If it doesn't it exits with 0. It should be 1 though because the file's created in the constructor...


#include "Settings.h"

using namespace std;

Settings *const Settings::_settings = NULL;

Settings::Settings()
: _in("settings.txt"), _fullScreen(false)
{
// handle error in opening file
if( !_in.good() )
{
_in.close();
std::ofstream out("settings.txt");
out << "fullscreen=true";
out.close();
_in.open("settings.txt");
}

ReadLines();
_in.close();

exit(_vec.size());

RemoveSpaces();
SetFullScreen();
}

void Settings::ReadLines()
{
string temp;
while( getline(_in, temp) ) // add text lines in the vector
_vec.push_back(temp);
}

void Settings::RemoveSpaces()
{
for(unsigned i = 0; i < _vec.size(); ++i)
{
string temp;
for(unsigned j = 0; j < _vec.size(); ++j)
if( !isspace(_vec[j]) )
temp += _vec[j];

_vec = temp;
}
}

void Settings::SetFullScreen()
{
unsigned pos0 = _vec[0].find("fullscreen"); // ->error here
pos0 += 9; // go at end of "fullscreen"

if( _vec[0].substr(pos0 + 2) == "true" )
_fullScreen = true;
else
_fullScreen = false;
}

Share this post


Link to post
Share on other sites
Try calling clear on your _in after you close it after seeing that it has failed to load the file:


// handle error in opening file
if( !_in )
{
_in.close();
_in.clear();


Share this post


Link to post
Share on other sites
Quote:
Original post by mattd
Try calling clear on your _in after you close it after seeing that it has failed to load the file:

*** Source Snippet Removed ***


Works, thanks :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!