方法一:
CFile類的成員變量:
m_hFile:表示一個打開文件的操作系統文件句柄。通過對m_hFile 與 CFile::hFileNull的比較來判斷該文件是否已經打開。
示例代碼:
1 CString strFilename = _T("D:\\大學語文.docx");
2 CFile file;
3 file.Open(strFilename,CFile::modeReadWrite);//
4
6 if (file.m_hFile != CFile::hFileNull)
7 {
8 file.Close();
9 }
10 else
11 {
12 printf("File Already Close \n");
13 }
方法二:
利用file.GetFileName().IsEmpty()來判斷
示例代碼
1 CString strFilename = _T("D:\\大學語文.docx");
2 CFile file;
3 file.Open(strFilename,CFile::modeReadWrite);//
4
5 if (!file.GetFileName().IsEmpty())
6 {
7 file.Close();
8 }
9 else
10 {
11 printf("File Already Close \n");
12 }
方法三:
通過設置成員變量來記錄文件是否被打開。如BOOL bIsFileOpen;默認是FALSE,
打開成功,把它置爲TRUE;否則置爲FALSE;
然後在程序裏面判斷就可以了。關閉後置bIsFileOpen爲FALSE,