共用方式為


存取檔案狀態

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

若要取得檔案狀態

  • 使用 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 );
}       

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

請參閱

概念

在 MFC 中的檔案