Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Boops

How to check if a file doesn't exist?

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

I use the following code to open and read a file (please ignore the fact that there are no std::''s):
quote:
long size = 37390; unsigned char buffer[size]; ifstream file (filename, ios::in|ios::binary|ios::ate); file.seekg (0, ios::beg); file.read (buffer, size); file.close();
But I don''t know how to check wether or not the file actually exists, how can that be done? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Here''s my code for checking the existence of a file.. I put it in a separate method:


bool CFichero::fileExists()
{
if (filePath!="")
{
ofstream aux(filePath, ios_base::in);

if(!aux) // if file does not exist...

{
return false;
}
else
{ // If file exists...

aux.close(); // ...close it

return true; }
}
else
{
// If the filePath is empty, error

cerr<<"
[CFile]: filePath==\"\""<<endl;
return false;
}
}



Hope that helps...


---
"To Iterate is human, To Recurse Divine..."

Share this post


Link to post
Share on other sites
You can also do it like this I think...a little easier to read.


if(!file.is_open())
//something wrong


doing it like "if(!file)" will work as well though...


[edited by - NullPointer33 on February 19, 2004 7:29:17 PM]

Share this post


Link to post
Share on other sites

ifstream in;
in.open("filename", ios::nocreate);
if (!in)
{
// no file exists"

}


"A soldier is a part of the 1% of the population that keeps the other 99% free" - Lt. Colonel Todd, 1/38th Infantry, Ft. Benning, GA

Share this post


Link to post
Share on other sites

  • 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!