How can we check if a file Exists or not using Win32 program?
WindowsWinapiFileWindows Problem Overview
How can we check if a file Exists or not using a Win32 program? I am working for a Windows Mobile App.
Windows Solutions
Solution 1 - Windows
Use GetFileAttributes
to check that the file system object exists and that it is not a directory.
BOOL FileExists(LPCTSTR szPath)
{
DWORD dwAttrib = GetFileAttributes(szPath);
return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}
Copied from How do you check if a directory exists on Windows in C?
Solution 2 - Windows
You can make use of the function GetFileAttributes
. It returns 0xFFFFFFFF
if the file does not exist.
Solution 3 - Windows
You can call FindFirstFile
.
Here is a sample I just knocked up:
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
int fileExists(TCHAR * file)
{
WIN32_FIND_DATA FindFileData;
HANDLE handle = FindFirstFile(file, &FindFileData) ;
int found = handle != INVALID_HANDLE_VALUE;
if(found)
{
//FindClose(&handle); this will crash
FindClose(handle);
}
return found;
}
void _tmain(int argc, TCHAR *argv[])
{
if( argc != 2 )
{
_tprintf(TEXT("Usage: %s [target_file]\n"), argv[0]);
return;
}
_tprintf (TEXT("Looking for file is %s\n"), argv[1]);
if (fileExists(argv[1]))
{
_tprintf (TEXT("File %s exists\n"), argv[1]);
}
else
{
_tprintf (TEXT("File %s doesn't exist\n"), argv[1]);
}
}
Solution 4 - Windows
How about simply:
#include <io.h>
if(_access(path, 0) == 0)
... // file exists
Solution 5 - Windows
Another option: 'PathFileExists'.
But I'd probably go with GetFileAttributes
.
Solution 6 - Windows
You can try to open the file. If it failed, it means not exist in most time.
Solution 7 - Windows
Came across the same issue and found this brief code in another forum which uses GetFileAttributes Approach
DWORD dwAttr = GetFileAttributes(szPath);
if (dwAttr == 0xffffffff){
DWORD dwError = GetLastError();
if (dwError == ERROR_FILE_NOT_FOUND)
{
// file not found
}
else if (dwError == ERROR_PATH_NOT_FOUND)
{
// path not found
}
else if (dwError == ERROR_ACCESS_DENIED)
{
// file or directory exists, but access is denied
}
else
{
// some other error has occured
}
}else{
if (dwAttr & FILE_ATTRIBUTE_DIRECTORY)
{
// this is a directory
}
else
{
// this is an ordinary file
}
}
where szPath
is the file-path.
Solution 8 - Windows
Use OpenFile
with uStyle
= OF_EXIST
if (OpenFile(path, NULL, OF_EXIST) == HFILE_ERROR)
{
// file not found
}
// file exists, but is not open
Remember, when using OF_EXIST
, the file is not open after OpenFile
succeeds. Per Win32 documentation:
Value | Meaning |
---|---|
OF_EXIST (0x00004000) | Opens a file and then closes it. Use this to test for the existence of a file. |
See doc: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-openfile
Solution 9 - Windows
Another more generic non-windows way:
static bool FileExists(const char *path)
{
FILE *fp;
fpos_t fsize = 0;
if ( !fopen_s(&fp, path, "r") )
{
fseek(fp, 0, SEEK_END);
fgetpos(fp, &fsize);
fclose(fp);
}
return fsize > 0;
}