Skip to main content
GameDev.net gamedev.net
🔒 Locked

determining if a file exists (C++)

Started by Mulligan Mar 8, 2004 at 12:41 AM 10 replies 59.2k views
Original Post
Mulligan
Mulligan
in c++ using simple i/o, how can i tell (given a file name) if the file exists? [edited by - Mulligan on March 8, 2004 1:42:00 AM]
Mulligan
Mulligan
thanks
Endurion
Endurion
Nice memory leak if the file exists. If the file exists you get a file handle, if you do, you ought to fclose it.
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
Jedyte
Jedyte
That was the C way. In C++ it''s

if(ifstream("fileName"))
cout << "exists!";
And the price we paid was the price men have always paid for achieving paradise in this life -- we went soft, we lost our edge. - "Muad'Dib: Conversations" by the Princess Irulan
sas
sas
if you are a windows programmer you can use this:

bool exists(const string& fileName)
{
bool returnValue = false;
DWORD attrib = GetFileAttributes(fileName.c_str());
if (attrib != 0xFFFFFFFF)
{
returnValue = true;
}
return returnValue;
}

iirc the file will not be opened by this code.

Arild Fines
Arild Fines
quote:
Original post by sas
bool exists(const string& fileName)
{
bool returnValue = false;
DWORD attrib = GetFileAttributes(fileName.c_str());
if (attrib != 0xFFFFFFFF)
{
returnValue = true;
}
return returnValue;
}

Would you by any chance have a programming teacher that smacks you over the head if you have more than one exit point from a function?
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
sas
sas
quote:
Original post by Arild Fines
quote:
Original post by sas
bool exists(const string& fileName)
{
bool returnValue = false;
DWORD attrib = GetFileAttributes(fileName.c_str());
if (attrib != 0xFFFFFFFF)
{
returnValue = true;
}
return returnValue;
}

Would you by any chance have a programming teacher that smacks you over the head if you have more than one exit point from a function?


You should read more carefully, Mister.
There is only one exit point.

EbonySeraph
EbonySeraph
I think the point of that comment was criticising your code. It''s written in such a way that is hard to read, but aims at making only one exit point. Basically, it seems like you tried too hard to make that happen.

And i think a simpler function would read

bool exists(const char * filename)
{
return GetFileAttributes(filename) != 0xFFFFFFFF;
}

Which does the same thing(end result).
"Ogun's Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.
Trap
Trap
The test doesn''t guarantee anything, files might be created or deleted at any time, especially between the test and the code that depends on the test.
Mulligan
Mulligan
wow, how did this turn into a flame war, i got the info i needed and, yes, i stuck a close() in there.

Thanks again
Promit
Promit
Sounds like Mulligan already has it, but just to make it coherent:

bool FileExists( const char* FileName )
{
FILE* fp = NULL;

//will not work if you do not have read permissions

//to the file, but if you don''t have read, it

//may as well not exist to begin with.

fp = fopen( FileName, "rb" );
if( fp != NULL )
{
fclose( fp );
return true;
}

return false;
}
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.