c++ 判断文件夹是否存在
2009-07-20 13:08
267 查看
(1)
//目录是否存在的检查:
bool CheckFolderExist(const string &strPath)
{
WIN32_FIND_DATA wfd;
bool rValue = false;
HANDLE hFind = FindFirstFile(strPath.c_str(), &wfd);
if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
rValue = true;
}
FindClose(hFind);
return rValue;
}
(2)PathFileExists("yourfile") 但是使用时候需要 #include "Shlwapi.h"
(3)
bool FileExists(LPCTSTR lpszFileName, bool bIsDirCheck)
{
DWORD dwAttributes = GetFileAttributes(lpszFileName);
if(dwAttributes == 0xFFFFFFFF)
{
return false;
}
if((dwAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
{
return bIsDirCheck;
}
else
{
return !bIsDirCheck;
}
}
(4)使用boost的filesystem类库的exists函数
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/convenience.hpp>
int GetFilePath(std::string &strFilePath)
{
string strPath;
int nRes = 0;
//指定路径
strPath = "D:/myTest/Test1/Test2";
namespace fs = boost::filesystem;
//路径的可移植
fs::path full_path( fs::initial_path() );
full_path = fs::system_complete( fs::path(strPath, fs::native ) );
//判断各级子目录是否存在,不存在则需要创建
if ( !fs::exists( full_path ) )
{
// 创建多层子目录
bool bRet = fs::create_directories(full_path);
if (false == bRet)
{
return -1;
}
}
strFilePath = full_path.native_directory_string();
return 0;
}
//目录是否存在的检查:
bool CheckFolderExist(const string &strPath)
{
WIN32_FIND_DATA wfd;
bool rValue = false;
HANDLE hFind = FindFirstFile(strPath.c_str(), &wfd);
if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
rValue = true;
}
FindClose(hFind);
return rValue;
}
(2)PathFileExists("yourfile") 但是使用时候需要 #include "Shlwapi.h"
(3)
bool FileExists(LPCTSTR lpszFileName, bool bIsDirCheck)
{
DWORD dwAttributes = GetFileAttributes(lpszFileName);
if(dwAttributes == 0xFFFFFFFF)
{
return false;
}
if((dwAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
{
return bIsDirCheck;
}
else
{
return !bIsDirCheck;
}
}
(4)使用boost的filesystem类库的exists函数
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/convenience.hpp>
int GetFilePath(std::string &strFilePath)
{
string strPath;
int nRes = 0;
//指定路径
strPath = "D:/myTest/Test1/Test2";
namespace fs = boost::filesystem;
//路径的可移植
fs::path full_path( fs::initial_path() );
full_path = fs::system_complete( fs::path(strPath, fs::native ) );
//判断各级子目录是否存在,不存在则需要创建
if ( !fs::exists( full_path ) )
{
// 创建多层子目录
bool bRet = fs::create_directories(full_path);
if (false == bRet)
{
return -1;
}
}
strFilePath = full_path.native_directory_string();
return 0;
}
相关文章推荐
- C++ 判断文件文件夹是否存在
- C/C++ 判断文件夹是否存在以及创建、删除文件夹 windows以及linux通用
- C++文件与文件夹操作(1)--判断文件和文件夹是否存在
- C++判断文件夹是否存在
- c++ 判断文件或文件夹是否存在
- C/C++判断文件文件夹是否存在
- C/C++ 判断文件夹是否存在以及创建、删除文件夹 windows以及linux通用
- C++判断文件夹是否存在
- C/C++判断文件/文件夹是否存在 转
- C/C++中判断文件或文件夹是否存在
- c++ 判断文件、文件夹(路径)是否存在
- C# C++ 判断文件 文件夹是否存在
- C/C++判断文件/文件夹是否存在
- C/C++判断文件/文件夹是否存在
- c++判断文件或文件夹是否存在--转载
- C++ 获取文件夹内的文件名,判断文件(夹)是否存在的两个函数
- C++判断文件夹是否存在
- 判断文件夹、文件是否存在、创建文件 c++ matlab
- c++判断文件夹是否存在,若不存在,调用linux命令创建它
- C/C++ 判断文件夹是否存在以及创建、删除文件夹 windows以及linux通用