• Advertisement
Sign in to follow this  

ifstream Getline Problem

This topic is 4797 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 All, I have a problem with the code below
void LoadRoomITEM(char * NAME,int ItemIndex)
{
	
	char cbuffer[256]; //Line input buffer
	char Filename[500]; //Path and filename

	//BUILD FILENAME
	strcpy(Filename,".\\ITEMS\\");
	strcat(Filename,NAME);
	strcat(Filename,".dat");

	ifstream iFile(Filename);

	if (!iFile.is_open())
	{ cout << "Error opening file"; exit (1); }//ERROR can not find room

	
	while (! iFile.eof() )
	{
		
		iFile.getline (cbuffer,100);
	        //...Do stuff
	}
	
	iFile.close();
	delete iFile;
}

when i run the function first time all works fine. when it enters it the second time it crashes and give the following error Unhandled exception at 0x77f813b1 in TextEng.exe: User breakpoint. This is on the iFile.Getline thanks Anthony [/source]

Share this post


Link to post
Share on other sites
Advertisement
You should seriously consider using std::string rather than c style char arrays. There is a stl function, std::getline, which will read a line from an ifstream into a string. Your function using string's would look like this:


void LoadRoomITEM(const std::string & NAME, int ItemIndex)
{
string cbuffer;
string Filename;

filename = ".\\ITEMS\\";
filename += NAME;
filename += ".dat";

ifstream iFile(Filename);

if (!iFile.is_open())
{
//ERROR can not find room
cout << "Error opening file";
exit (1);
}

while (!iFile.eof())
{
std::getline(iFile, cbuffer);
// Do stuff
}
}



Also, you dont actually need to close the file. It will close automatically when it goes out of scope (that is, when the function returns). Also, you definatly should not be delete'ing iFile. You only ever delete something that you created with new.

Alan

Share this post


Link to post
Share on other sites
Quote:
Original post by AnthonyN
Unhandled exception at 0x77f813b1 in TextEng.exe: User breakpoint.

This is on the iFile.Getline

thanks
Anthony
Can't you just delete the breakpoint?

Share this post


Link to post
Share on other sites
Quote:
Original post by AnthonyN
Thanks for the info, but that is not solving my problem...

Why is it doing it?


I dont know. There is nothing wrong with the code you posted.

Does the crash happen the first time through the while loop? What are you doing where you say "...Do stuff"?

Alan

Share this post


Link to post
Share on other sites
It is the First time through the loop so it does not get to the code in do stuff. just to getline then the error message.

PS I have commented out all other code just to make sure

Share this post


Link to post
Share on other sites
Alan

Thanks for your help, I have found the error and it wa nothing to do with ifstream.It was when adding an item to a class.

Don't you just love c++ errors ;)

Share this post


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

  • Advertisement