Does the file exists

After about 16 years of writing C++ on Windows I’m sure there’s been many an occasion with the need to test if a file exists.  Today, I discovered a new way to check!

bool FileExists(const extra::tstring &file)
   OFSTRUCT of = {sizeof(of)};
   return (OpenFile(CT2CA(file.c_str()),&of,OF_EXIST)!=HFILE_ERROR);

Where an extra::tstring is declared such (part of my library of code):

typedef std::basic_string<TCHAR>tstring;

Apparently it closes the handle (see docs), but I still get nervous ignoring a handle like that.  The downside (handle anxieties aside) is that this OpenFile function, which I don’t recall ever using before, does not have a Unicode version (hence the need for CT2CA).


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s