判断文件CFile已经打开

判断文件CFile已经打开
方法一:

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;

然后在程序里面判断就可以了。关闭后置bIsFileOpenFALSE,

 

你可能感兴趣的:(判断文件CFile已经打开)