home » knowledge base » Check if file exists on Windows (2006-01-02)

Tags: c (9) code snippet (6) windows (8)

Check if file exists on Windows

Windows doesn't have a built-in function that checks if a file with a given name exists. It can be trivially written using GetFileAttributes or FindFirstFile APIs. Version below uses GetFileAttributes.

/* Return TRUE if file 'fileName' exists */
int FileExists(const TCHAR *fileName)
{
    DWORD       fileAttr;

    fileAttr = GetFileAttributes(fileName);
    if (0xFFFFFFFF == fileAttr)
        return false;
    return true;
}
Tags: c (9) code snippet (6) windows (8)


Krzysztof Kowalczyk