方法一:
CFile类的成员变量:
m_hFile:表示一个打开文件的操作系统文件句柄。通过对m_hFile 与 CFile::hFileNull的比较来判断该文件是否已经打开。
示例代码:
CString strFilename = _T("D:\\大学语文.docx");
CFile file;
file.Open(strFilename,CFile::modeReadWrite);
if(file.m_hFile != CFile::hFileNull)
{
file.Close();
}
else
{
printf("File Already Close \n");
}
方法二:
利用file.GetFileName().IsEmpty()来判断
示例代码:
CString strFilename = _T("D:\\大学语文.docx");
CFile file;
file.Open(strFilename,CFile::modeReadWrite);
//
if(!file.GetFileName().IsEmpty())
{
file.Close();
}
else
{
printf("File Already Close \n");
}
方法三:
通过设置成员变量来记录文件是否被打开。如BOOL bIsFileOpen;默认是FALSE,
打开成功,把它置为TRUE;否则置为FALSE;
然后在程序里面判断就可以了。关闭后置bIsFileOpen为FALSE,
转载地址:http://www.cppblog.com/wanghaiguang/archive/2013/09/23/203381.html
转载于:https://blog.51cto.com/3994129/1596004