RK-
RK-

Reputation: 12221

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

How can we check if a file Exists or not using a Win32 program? I am working for a Windows Mobile App.

Upvotes: 83

Views: 102728

Answers (9)

srpax
srpax

Reputation: 21

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://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-openfile

Upvotes: 0

A5H1Q
A5H1Q

Reputation: 624

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.

Upvotes: 1

codaddict
codaddict

Reputation: 455400

You can make use of the function GetFileAttributes. It returns 0xFFFFFFFF if the file does not exist.

Upvotes: 39

Zach Burlingame
Zach Burlingame

Reputation: 13806

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?

Upvotes: 231

Preet Sangha
Preet Sangha

Reputation: 65556

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]);
   }
}

Upvotes: 29

Pierre
Pierre

Reputation: 4414

How about simply:

#include <io.h>
if(_access(path, 0) == 0)
    ...   // file exists

Upvotes: 19

Alturis
Alturis

Reputation: 74

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;
}

Upvotes: -2

Adrian McCarthy
Adrian McCarthy

Reputation: 48028

Another option: 'PathFileExists'.

But I'd probably go with GetFileAttributes.

Upvotes: 10

fanzhou
fanzhou

Reputation: 97

You can try to open the file. If it failed, it means not exist in most time.

Upvotes: 0

Related Questions