Jump to content
  • Advertisement
Sign in to follow this  
deathwearer

Trouble to read another txt files with my class

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

Ok, i have a class that handle the reading of my txt files. I can open open a file once, but when i try to open another files after ( with the same ifstream object ) it won't sucess to read the other file. I close the file each time i finish reading. What am i doing wrong?
void CReading::OpenMap(string sMap)
{
         //m_ifMap.close() have been called already
	m_ifMap.open(sMap.c_str(), ios::in );
	if (!m_ifMap.fail()) //If fail to open
	{
		SetCanRead(true);
	}
	else
	{
		m_ifMap.clear();
		SetCanRead(false);
	}
}

Share this post


Link to post
Share on other sites
Advertisement
My strong recommendation is not to try to hold an ifstream object as a class member, but instead create a local one when you need it. That will avoid the problem you are having, which is that you need to .close() an ifstream before you can .open() it with a new file. (With the local, the stream will be implicitly be .close()d by its destructor, and on the next call you get a fresh ifstream for the next file anyway.)

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!