分享方式:


存取檔案狀態

CFile 也支援取得檔案狀態,包括檔案是否存在、建立和修改日期和時間、邏輯大小和路徑。

取得檔案狀態

  1. 使用 CFile 類別來取得和設定檔案的相關資訊。 其中一個有用的應用程式是使用 CFile 靜態成員函 式 GetStatus 來判斷檔案是否存在。 如果指定的檔案不存在,GetStatus 會傳回 0。

因此,您可以使用 GetStatus 的結果 來判斷在開啟檔案時是否要使用 CFile::modeCreate 旗標,如下列範例所示:

CFile theFile;
TCHAR* szFileName = _T("c:\\test\\myfile.dat");
BOOL bOpenOK;

CFileStatus status;
if( CFile::GetStatus( szFileName, status ) )
{
   // Open the file without the Create flag
   bOpenOK = theFile.Open( szFileName, 
      CFile::modeWrite );
}
else
{
   // Open the file with the Create flag
   bOpenOK = theFile.Open( szFileName, 
      CFile::modeCreate | CFile::modeWrite );
}

如需相關資訊,請參閱 序列化

另請參閱

檔案