c/c++ 检查指定目录是否存在,不存在则创建改目录

来源:互联网 发布:外汇交易大师软件 编辑:程序博客网 时间:2024/04/28 06:57
 1 #include <windows.h> 2 #include <string>  3 #include <vector> 4 using namespace std;  5  6 // 核查目录,若目录不存在,创建目录 7 bool FindOrCreateDirectory( const char* pszPath ) 8 { 9     WIN32_FIND_DATA fd;10     HANDLE hFind = ::FindFirstFile( pszPath, &fd );11     while( hFind != INVALID_HANDLE_VALUE )12     {13         if ( fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )14             return true;15     }16 17     if ( !::CreateDirectory( pszPath, NULL ) )18     {19         char szDir[MAX_PATH];20         sprintf_s( szDir, sizeof(szDir), "创建目录[%s]失败,请检查权限", pszPath );21         ::MessageBox( NULL, szDir, "创建目录失败", MB_OK|MB_ICONERROR );22         return false;23     }24 25     return true;26 }

 

0 0
原创粉丝点击