Archived

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

How to check if a file doesn't exist?

This topic is 5044 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
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
boost::filesystem::exist

- Magmai Kai Holmlor

Not For Rent

[Look for information | GDNet Start Here | GDNet Search Tool | GDNet FAQ | MSDN RTF[L] | SGI STL Docs | STFW | Asking Smart Questions ]
[Free C++ Libraries | Boost | ACE | Loki | MTL | Blitz++ | wxWindows| Spirit(xBNF)]
[Free C Libraries | zlib ]

Share this post


Link to post
Share on other sites