Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

problem reading and printing from file

$
0
0
I am trying to read to a file that is not there so it just creates a .txt with the first highscore in it, then when it reads from file it appears when file is open it is pointing at 0xcccccccc. i am trying to get this code to just grab old values compare new high scores and then put the 5 highest back into the .txt but when it runs the second time to put in the second score it never reaches end of file and never takes in any values... why is this happeneing and how can i correct it?

** File struct contains a string for name and unsigned int score and num correct **

	fstream file;
File IO;
	vector<File> IoS;
		unsigned int k = 0;
	file.open("highscores.txt", ios_base::in);
	IoS.clear();
	if(file.is_open())
	{
		while (!file.eof())
		{
			getline(file, IO.name, '\t');
			file << IO.score;
			file.ignore(INT_MAX, '\t');
			file << IO.corr;
			file.ignore(INT_MAX, '\n');
			IoS.insert(IoS.begin() +k, IO);
			//IoS.push_back(IO);
			IO.corr = IO.score= NULL;
			IO.name = "";
		}
	}
	file.close();
	bool insert = false;

	for (int i = 0; i < IoS.size(); i++)
	{
		if (IoS[i].score <= playerscore )
		{
		 insert = true;
		 k = i;
		 break;
		}

	}
	
			IO.corr = numcorr;
			IO.name = playername;
			IO.score = playerscore;
			IoS.insert(IoS.begin() +k, IO);


			file.open("highscores.txt", ios_base::out  );
	if(file.is_open())
	{
		for (int i = 0; i < IoS.size(); i++)
		{
			if (i == IoS.size()-1)
			{
file << IoS[i].name << '\t' << IoS[i].score << '\t' << IoS[i].corr;
			}
			else
			{
file << IoS[i].name << '\t' << IoS[i].score << '\t' << IoS[i].corr << '\n';
			}
			

		}
	}
	file.close();

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>