C++判断文件夹是否存在
2014-10-22 10:18
134 查看
判断文件夹是否存在
一、判断文件夹是否存在:
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;
}
一、判断文件夹是否存在:
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;
}
相关文章推荐
- C++遍历文件夹及判断某一文件或目录是否存在
- C/C++判断文件文件夹是否存在
- C++ 获取文件夹内的文件名,判断文件(夹)是否存在的两个函数
- C/C++ 判断文件夹是否存在以及创建、删除文件夹 windows以及linux通用
- VC C++ 判断文件夹中是否存在某个文件
- 判断文件夹、文件是否存在、创建文件 c++ matlab
- c++判断文件或文件夹是否存在--转载
- C++文件与文件夹操作(1)--判断文件和文件夹是否存在
- C/C++判断文件/文件夹是否存在
- C++ - 判断文件夹(folder)是否存在(exist)
- [C/C++相关]C语言判断文件夹或者文件是否存在的方法
- C++判断文件夹是否存在
- C/C++ 判断文件夹是否存在以及创建、删除文件夹 windows以及linux通用
- C++ 判断文件文件夹是否存在
- C++用Windows API判断文件或文件夹是否存在
- C/C++ 判断文件夹是否存在以及创建、删除文件夹 windows以及linux通用
- c++ 判断文件或文件夹是否存在
- c++ 判断文件夹是否存在
- C/C++判断文件/文件夹是否存在 转
- C/C++中判断文件或文件夹是否存在