5 Replies - 329 Views - Last Post: 23 January 2013 - 05:01 PM Rate Topic: -----

#1 MDuff0113  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 06-March 12

problem reading and printing from file

Posted 22 January 2013 - 10:57 PM

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();


Is This A Good Question/Topic? 0
  • +

Replies To: problem reading and printing from file

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,426
  • Joined: 25-December 09

Re: problem reading and printing from file

Posted 22 January 2013 - 11:09 PM

Please post a small sample of your input file.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 MDuff0113  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 06-March 12

Re: problem reading and printing from file

Posted 23 January 2013 - 05:09 AM

View Postjimblumberg, on 22 January 2013 - 11:09 PM, said:

Please post a small sample of your input file.

Jim


My input file starts with nothing in ut then after first time it prints this to file: No Name 0 0. but second time it gets stuck in the while !eof check
Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 770
  • View blog
  • Posts: 2,249
  • Joined: 20-March 10

Re: problem reading and printing from file

Posted 23 January 2013 - 06:24 AM

Hi,

The function returns true if the eof bit stream's error flag has been set by a previous i/o operation. This flag is set by all standard input operations when the End Of File is reached in the sequence associated with the stream.

so either use

while(file.eof())

or while(!file.good())

The first time you use it the eof returns false but the second time the function always returns true
so it loops continously as .eof() is always true

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,426
  • Joined: 25-December 09

Re: problem reading and printing from file

Posted 23 January 2013 - 08:07 AM

Quote

I am trying to read to a file that is not there so it just creates a .txt

Since you are reading a fstream, with the default open mode, the file must exist. You need to use the std::ios::app open mode to force a fstream to open a non-existing file.

Note if the file exists in your first loop you read until the end of file. This action sets an error state with your stream. So after that point your file will appear to reopen correctly but you will never write anything to the file because of this error condition. You need to clear your error condition before you try to re-use the stream. Closing and the re-opening the file will not clear the error stream.

Lastly you should not be using eof() or good() to control your reading loops. This will usually cause problems. One of the problems is you will usually get one extra line of inupt (the last line repeats). You should use the read operation of your file instead.

In the following snippet:
	file.open("highscores.txt", ios_base::in);
	if(file.is_open())
	{
		while (!file.eof())
		{
		}
	}
	file.close();

The if statement is actually redundant, because you don't actually do anything about the fact that the file didn't open. In this case the while wouldn't actually do anything, other than set the eof() flag. So really your input loop could look like:
	file.open("highscores.txt", ios_base::in);
	while (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();

The read operation will read a line until either the tab character or the end of line character is found. The next operation file << IO.score; will probably fail since you opened your file for input only. You can't write to an input file. The operator<< is the insertion operator, meaing you are trying to write the the file. You probably want to use the operator>>, the extraction operatior to retrieve the next item.

Also trying to read and write to a file at the same time can be tricky, until you are more familiar with these operations I suggest you stick to either reading or writing to the file, don't mix the operations.

Without seeing a sample of your input file it is very difficult to actually tell where your problems will actually appear. However you may want to consider using getline() to extract the complete line, then use a stringstream to parse this line.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 MDuff0113  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 06-March 12

Re: problem reading and printing from file

Posted 23 January 2013 - 05:01 PM

[quote name='jimblumberg' date='23 January 2013 - 08:07 AM' timestamp='1358953627' post='1788804']

Quote

The read operation will read a line until either the tab character or the end of line character is found. The next operation file << IO.score; will probably fail since you opened your file for input only. You can't write to an input file. The operator<< is the insertion operator, meaing you are trying to write the the file. You probably want to use the operator>>, the extraction operatior to retrieve the next item.

Also trying to read and write to a file at the same time can be tricky, until you are more familiar with these operations I suggest you stick to either reading or writing to the file, don't mix the operations.

Without seeing a sample of your input file it is very difficult to actually tell where your problems will actually appear. However you may want to consider using getline() to extract the complete line, then use a stringstream to parse this line.

Jim


yes before i checked back to this post i noticed that the << should have been >> so it was infinately writing to the file. Thought what i meant is that the game starts without a .txt so the first time it never reads in it just prints out player info to it then on second plus playthroughs it will read in from that file and compare scores. Though thank you your extra info has been very helpful
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1