Problem with fstream

Started by
13 comments, last by raccoonone 18 years, 4 months ago

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?
Advertisement
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();			}
Gor435 - My Journal - MySpace - Facebook
Oh! I thought that I needed to .close an ifstream before I could call .open again. Thanks:)
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.
Gor435 - My Journal - MySpace - Facebook
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.
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.
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.
--> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--
Quote:Original post by Chris41411
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.


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?
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();}
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.

This topic is closed to new replies.

Advertisement