一、判断文件夹是否存在:
1.用CreateDirectory(".//FileManege",NULL);如果文件夹FileManege不存在,则创建。
2.或者if(_access(".//FileManege",0)==-1),表示FileManege不存在。
3.或者BOOL PathIsDirectory(LPCTSTR pszPath);
二、判断文件是否存在:
1.用if((file=fopen(".//FileManege//F//F.dat","rb"))==NULL)
file=fopen(".//FileManege//F//F.dat","ab+"); // 先判断有无文件,没的话新建一个
2.用if(_access(".//FileManege//F//F.dat",0)==-1),表示文件不存在。
函数int _access( const char *path, int mode );可以判断文件或者文件夹的mode属性
mode=00;//Existence only
mode=02;//Write permission
mode=04;//Read permission
mode=06;//Read and write permission
需要包含头文件<io.h>。
三、例子
判断文件夹是否存在若不存在创建
bool CheckFolderExist( const string & strPath)
{
WIN32_FIND_DATA wfd;
bool rValue = false ;
HANDLE hFind = FindFirstFile((LPCWSTR)strPath.c_str(), & wfd);
if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
rValue = true ;
}else
{
if(_mkdir(strPath.c_str())==0)
{
return 1;//文件夹创建成功
}
else
{
return -1;//can not make a dir;
}
}
FindClose(hFind);
return rValue;
}