Sign in to follow this  

inputing from a text file keeps crashing

This topic is 4132 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

I'm trying to load a series of names and scores from a text file. The file is ordered so that theres a name than a score on each line seperated with a '?'. here's my same plae file I'm trying to load. small ? 500 Long Name ? 34009 01234567890123456789 ? 9909876 1 1 1 1 1 1 1 1 1 1 1 1 1 ? 666666 Last One ? 0 Here's the code:
void CBEHighscores::Load()
{
	ifstream file;

	file.open("Highscores.txt");

	// read the entire file, hopeing the player hasn't altered it in a way that
	// will cause it to crash, start by reading in the name of the player, by 
	// takeing in chunks of strings until a ? is reached, following this should
	// the score corresponding to the name. then continu the while loop with 
	// the next score.

	int index = 0; // the index within the buffers to keep track of wich score we're on

	while (!file.eof())
	{
		string input = "x"; 
		int score;

		while (input[0] != '?')
		{
			file >> input;

			if (input[0] != '?')
			{
				// the input is a piece of the name we then need to check wether or
				// not there was a space infront of this piece, by checking if the
				// current name we're building already has other parts, if it's
				// size is greater than zero we add a space before this piece.
				if (m_names_buffer[index].length() > 0)
				{
					m_names_buffer[index].append(" ");
					m_names_buffer[index].append(input);
				}
				else
				{
					m_names_buffer.push_back(input);
				}
			}
		}

		// once we've read the '?' (seperatore) we just read in the score and
		// increment index to read in the next name and score;
		file >> score;

		m_scores_buffer.push_back(score);

		index++;
	}

	file.close();
}

By commenting everything out bit by bit, I believe it has something to do with how I seperate everything so when it comes to the point to take the integer it's still takeing in text, but even commenting out the integer stuff it crashes, the only thing I seem to be able to figure aout is it's stars when the While()'s start. Thanks for any help.

Share this post


Link to post
Share on other sites
The problem is not in the file IO, but in the vectors you're using. Make sure the vectors have enough space reserved for the names/scores you're loading. I got the code you provided to compile and run properly after inititalizing the vectors like:
vector<string> m_names_buffer(5); 
vector<int> m_scores_buffer(5);

I used 5 just for testing, as there is only 4 lines in the file, but one of the good things about vectors is you can make them bigger as you need.
That fixed the crashes and the data appeared to have loaded correctly ;)

Share this post


Link to post
Share on other sites
I figured this would fix that problem.


if (m_names_buffer.size() < index)
{
m_names_buffer.push_back(input);
}
else
{
m_names_buffer[index].append(" ");
m_names_buffer[index].append(input);
}




now I changed the above with this and it works perfectly


if (first_pass)
{
m_names_buffer.push_back(input);
}
else
{
m_names_buffer[index].append(" ");
m_names_buffer[index].append(input);
}

Share this post


Link to post
Share on other sites

This topic is 4132 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this