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

后端 未结 7 1537
傲寒
傲寒 2020-11-27 05:16

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

7条回答
  •  温柔的废话
    2020-11-27 06:04

    You can call FindFirstFile.

    Here is a sample I just knocked up:

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

提交回复
热议问题