Question : CreateFile in EVC

BOOL  CKnockClientDlg::FileExists (LPCTSTR lpszFilename)
{
      HANDLE hFile = CreateFile(lpszFilename, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);      
      // if the HANDLE returns invalid handle , means that the file already exists
      if (hFile != INVALID_HANDLE_VALUE)
      {
            
            CloseHandle(hFile);
            return TRUE;
      }
      else
      {
            return FALSE;
      }
      
}





      if(!FileExists (LPCTSTR("\\My Documents\\WB\\PDAIp.txt")))
      {
            
            stream = fopen( "\\My Documents\\WB\\PDAIp.txt", "w+" );
            fclose(stream);
            
      }


Hi , What i'm trying to do here is to check if the file exists and if it does not , create the file , if it already exists , leave it alone. The situation here is that it always says that the file does not exists... This set of codes works for VC++ 6.0 , but does not work for EVC++ 3.0 ... could anyone tell me where the problem lies?  many thanks...  =)

Answer : CreateFile in EVC

Alex is correct.  That technique is fast and effective.
Since this is the MFC topic area, you can use this:

      CFileStatus rStat;
      BOOL fRet= CFile::GetStatus("c:\\temp\\rpt.txt", rStat );
      if ( ! fRet ) Messagebox("It's NOT there!");

Also, what MFC does is this:

      WIN32_FIND_DATA findFileData;
      HANDLE hFind = FindFirstFile((LPTSTR)lpszFileName, &findFileData);
      if (hFind == INVALID_HANDLE_VALUE)
            return FALSE;
      FindClose(hFind);
      return TRUE;
Random Solutions  
 
programming4us programming4us