开发者

C++: Which is the best method of checking for file existence on windows platform [duplicate]

This question already has answers here: Closed 1开发者_如何学C0 years ago.

Possible Duplicate:

How can we check if a file Exists or not using Win32 program?

Which is the best method for checking for file existence:

Option1:

GetFileAttributes("C:\\MyFile.txt"); // from winbase.h
if(0xffffffff == GetFileAttributes("C:\\MyFile.txt"))
{
    //File not found
}

Option2:

std::string fileName("C:\\MyFile.txt" );
ifstream fin( fileName.c_str() );

if( fin.fail() )
{
    //File not found
}

Also if you think option 1 is the better method, can you tell me how to define 0xffffffff as a constant (I don't want to use #define)

Thanks


Note that GetFileAttributes() may fail for other reasons than lack of existence (for example permissions issues). I would add a check on the error code for robustness:

GetFileAttributes("C:\\MyFile.txt"); // from winbase.h
if(INVALID_FILE_ATTRIBUTES == GetFileAttributes("C:\\MyFile.txt") && GetLastError()==ERROR_FILE_NOT_FOUND)
{
    //File not found
}


There are two things to consider here:

  1. Checking if the file exists via its attributes is potentially many orders of magnitude faster - If a file exists on a 'slow' file system - tape, network storage, cd etc then opening the file will involve an actual round trip to the files location. The files attributes on the other hand are queried and cached by the filesystem drivers when the directory is queried, so probing file attributes involves a once off directory enumeration cost - meaning far fewer round trips - which can be a significant saving if multiple "slow" files are being checked.

  2. However, the files attributes merely indicate that the file existed at the time the call was made. The file can be deleted, or you might not haver permissions to access it. If you are about to try and open the file anyway, it would be better to dispense with the file attributes check and actually try and open the file.


If you're targeting only Windows, option #1 is clearly the better of the two. Also, the constant you're looking for already exists in the Windows headers -- it's called INVALID_FILE_ATTRIBUTES.


How about use boost?

if (!boost::filesystem::exists("C:\\MyFile.txt"))
{
    ...
}

http://www.boost.org/doc/libs/1_43_0/libs/filesystem/doc/index.htm


The only robust way to check whether a file exists is to try to open it, and see if it succeeds or fails.

Any other method is a potential race condition. For example, GetFileAttributes can tell you if a file existed when you called the function, but that's not the same as whether it exists when you try to open it. It might have been deleted (or created) in the meantime.


I would prefer the first. The second checks if the file can be opened, while a file might exist without you having the rights to open it.

You can use the INVALID_FILE_ATTRIBUTES constant.


There's a nice shell utility function for that, PathFileExists.


I'd opt to use the iostream because of its platform independence. True, you may be targeting Windows only, but it's never a bad move to use platform-independent libraries. If you're building a big project, you never know what that next phase will entail.

I'm not sure the code above is the best (or correct) way to check for file existence. Try this:

ifstream f;
f.open(str);

if (f.is_open()) {
  // read file
}


Back in the day, all we had was OpenFile (and we were GRATEFUL)

OFSTRUCT of = {0};
of.cBytes = sizeof(of);

HFILE hf = ::OpenFile("c:\\windows\\write.exe",&of,OF_EXIST);

if(hf > 0)
    printf("file exists"); 

return 0;


I also like to confirm that the string isn't a directory:

DWORD attr = GetFileAttributes(file);
if(attr == INVALID_FILE_ATTRIBUTES || (attr & FILE_ATTRIBUTE_DIRECTORY))
    return false;   //  not a file
return true;


#include <iostream>
#include <windows.h>
using namespace std;

bool FileExists(const TCHAR *fileName)
{
    DWORD       fileAttr;

    fileAttr = GetFileAttributes(fileName);
    if (0xFFFFFFFF == fileAttr && GetLastError()==ERROR_FILE_NOT_FOUND)
        return false;
    return true;
}

int main(int argc, char *argv[])
{


    if ( FileExists("testtt.txt"))
    {
      cout << "File exists \n";
    }
    else
    {
        cout << "File does not exist \n";

    }


  return 0;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜