方法一:
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,
posted on 2013-09-23 16:46
王海光 閱讀(10031)
評論(0) 編輯 收藏 引用 所屬分類:
MFC