• 11
• 14
• 19
• 27
• 9

[C++] problem reading settings from text file

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

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 on other sites
What is the exact error you get, and what line of code does it occur on?

Share on other sites
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 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 on other sites
Quote:
 Original post by mattdTry calling clear on your _in after you close it after seeing that it has failed to load the file:*** Source Snippet Removed ***

Works, thanks :)