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();
}
Problem with fstream
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(); }
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.
I changed it to
However I'm still having the same problem. After openning the first file the rest of them don't open.
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:
You need to include fcntl.h, sys/stat.h, io.h for declarations and constants of these functions.
for(long i = 0; i < 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 << m_TexturesInFile.sName << " does not exist" << 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.
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
Popular Topics
Advertisement