Public Group

Problem with fstream

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

Recommended Posts

for(long i = 0; i < m_TexturesInFile.Len(); i++)
{
sTemp = "Textures\\";
sTemp += m_TexturesInFile.sName;
fin.open(m_TexturesInFile.sName.data());
if(!fin.is_open())
{
if(bOutput)
{
cout << m_TexturesInFile.sName << " does not exist" << endl;
}
bError = true;
}
fin.clear();
fin.close();
}


This is the code I'm using. However after it opens and closes the first file, it fails to open the rest (there are about 10 more). I've checked to make sure the files exist, and used the debugger to check that it's trying to open the correct files. I've had this problem before where I'm not able to reopen an fstream after I've closed it. Do I need to call some other functions besides .clear() and .close() before I reopen with a new name?

Share on other sites
try this.

for(long i = 0; i < m_TexturesInFile.Len(); i++)	{		sTemp = "Textures\\";		sTemp += m_TexturesInFile.sName;                ifstream fin;		fin.open(m_TexturesInFile.sName.data(), ios::in | ios::binary);		if(!fin))		{			if(bOutput)			{				cout << m_TexturesInFile.sName << " does not exist" << endl;			}			bError = true;		}                //fin.clear();                fin.close();			}

Share on other sites
Oh! I thought that I needed to .close an ifstream before I could call .open again. Thanks:)

Share on other sites
I edited my post a bit check it out and see how that works for you it is the loading meathod I use in a tile engine I am working on.

Share on other sites
I changed it to
for(long i = 0; i < m_TexturesInFile.Len(); i++)	{		sTemp = "Textures\\";		sTemp += m_TexturesInFile.sName;		fin.open(m_TexturesInFile.sName.data(), ios::in);		if(!fin)		{			if(bOutput)			{				cout << m_TexturesInFile.sName << " does not exist" << endl;			}			bError = true;		}	}	fin.close();

However I'm still having the same problem. After openning the first file the rest of them don't open.

Share on other sites
I've been trying to deal with a similar problem where I have an ifstream object in my database class and after it has been closed once, it will not open the file again. I made it into a pointer to an ifstream object, and created a new instance and deleted it each time that I was done with it, and that seems to work, but I would like to know why after closing the file it won't open again.

Share on other sites
Instead of using ifstream, you may take a look at _open () and _close (). Okay, these are obsolete, but I often use them in throw-away code by their simplicity:

for(long i = 0; i &lt; m_TexturesInFile.Len(); i++)	{		sTemp = "Textures\\";		sTemp += m_TexturesInFile.sName;		//fin.open(m_TexturesInFile.sName.data());                int iFHandle = _open (sTemp.c_str (), _O_BINARY) ;		//if(!fin.is_open())                if (iFHandle == -1)		{			if(bOutput)			{				cout &lt;&lt; m_TexturesInFile.sName &lt;&lt; " does not exist" &lt;&lt; endl;			}			bError = true;		}		//fin.clear();		_close (iFHandle) ;	}

You need to include fcntl.h, sys/stat.h, io.h for declarations and constants of these functions.

Share on other sites
Quote:
 Original post by Chris41411I've been trying to deal with a similar problem where I have an ifstream object in my database class and after it has been closed once, it will not open the file again. I made it into a pointer to an ifstream object, and created a new instance and deleted it each time that I was done with it, and that seems to work, but I would like to know why after closing the file it won't open again.

I just searched on google, and it seems that the problem we've both had isn't uncommon. I wonder if there's something actually wrong with the <fstream> code?

Share on other sites
Well what is the whole point of sTemp? It looks like you add the "Textures\\" prefix to it before adding on the texture name, but you do not use it in your code. Anyways, have you tried moving the ifstream into the loop so it's destroyed and recreated as needed?

for(long i = 0; i < m_TexturesInFile.Len(); i++){        ifstream fin;	sTemp = "Textures\\";	sTemp += m_TexturesInFile.sName;	fin.open(m_TexturesInFile.sName.data());	if(!fin.is_open())	{		if(bOutput)		{			cout << m_TexturesInFile.sName << " does not exist" << endl;		}		bError = true;	}	// fin.clear();	fin.close();}

Share on other sites
Well, actually, I just figured out that I wasn't clearing the stream after I had reached the eof state, which causes the stream to be unusable until it is cleared.

1. 1
2. 2
3. 3
frob
15
4. 4
5. 5

• 20
• 12
• 13
• 14
• 80
• Forum Statistics

• Total Topics
632144
• Total Posts
3004399

×