Public Group

Trouble to read another txt files with my class

This topic is 4506 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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.open(sMap.c_str(), ios::in );
if (!m_ifMap.fail()) //If fail to open
{
}
else
{
m_ifMap.clear();
}
}



Share on other sites
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 on other sites
Alright I'll do that. Thank.

• 15
• 9
• 13
• 41
• 15