GetStatus( LPCTSTR lpszFileName, CFileStatus& rStatus );The static version gets the status of the named file and copies the filename to m_szFullName. This function obtains the file status from the directory entry without actually opening the file. It is useful for testing the existence and access rights of a file.
最简单的函数调用:if(_access("filename", 0)==-1) { TRACE("File not exit."); }
如果是Win32编程推荐用CreateFile 它里面有一个参数dwCreationDisposition专门就是为这个设计的,而且如果存在可以立即获得文件句柄,立即进入下一步操作 dwCreationDisposition : OPEN_EXISTING Opens the file. The function fails if the file does not exist. For a discussion of why you should use OPEN_EXISTING for devices, see Res
{
BOOL bExist = TRUE;
HANDLE hFind;
WIN32_FIND_DATA dataFind;
BOOL bMoreFiles = TRUE;
hFind = FindFirstFile(pszFile,&dataFind);
if(hFind== INVALID_HANDLE_VALUE)
{
bExist = FALSE;
}
FindClose(hFind);
return bExist;
}
fp=fopen(szFileName,"r");//szFileName是文件名
if(fp == NULL)
TRACE("文件不存在!");
否则,存在
BOOL CConfigDlg::IsDevFileExist(CString devname)
{
CFileFind tempFind;
CString path;
CString str, str1; path = g_strDevicePath + "*.*"; /* 得到当前目录 */ BOOL bIsFinded =(BOOL)tempFind.FindFile(path); /* 判断目录下是否为空 */ while(bIsFinded)
{
bIsFinded = (BOOL)tempFind.FindNextFile();
if( !tempFind.IsDots() )
{
str = tempFind.GetFileName();
str1.Format( "%s%s", devname, ".txt" );
if( str != str1 )
continue;
else
{
tempFind.Close();
return true;
}
}
}
tempFind.Close();
return false;
}
{
TRACE("File not exit.");
}
BOOL bFind=file.FindFile(".\\*.ini");
if(!bFind) MessageBox("缺少配置文件!","提示");
它里面有一个参数dwCreationDisposition专门就是为这个设计的,而且如果存在可以立即获得文件句柄,立即进入下一步操作
dwCreationDisposition :
OPEN_EXISTING Opens the file. The function fails if the file does not exist.
For a discussion of why you should use OPEN_EXISTING for devices,
see Res