

码龄10年
暂无认证
34134访问
1
等级
8获赞
3
评论
热门文章
-
一.某岛之人物对象及其加密函数解析
2147
-
一.《传奇M》装备栏遍历的突破口
1790
-
二.某龙端游中LUA的分析和调用
1539
-
一.《轩辕传奇》周围遍历之二叉树
1386
-
一.《UE4奥丁》人物最大属性
1331
最新评论
-
20240228阿⑤
积分乍获得
-
20231124ziher
666
-
20230909八月老师
什麼時候發佈易語言支持庫呢
一.《WIN32》判断文件夹是否存在
Heart
2023-08-29 09:08:48 发布
308
分类专栏: 开发技术 文章标签: 判断文件夹是否存在
判断文件代码:
///////////////////////////////////////////////////////////////////////Win102 #ifdef WIN32 #include //C (Windows) access #else #include //C (Linux) access #endif ///////////////////////////////////////////////////////////////////////C++ #include //C++ fstream ///////////////////////////////////////////////////////////////////////WIN32 #ifdef WIN32 #include //Windows API FindFirstFile #include #pragma comment(lib, "shlwapi.lib") //Windows API PathFileExists #endif ///////////////////////////////////////////////////////////////////////boost #include //boost using namespace std; int main() { char file_name[] = "C://迪大学院.txt"; /////////////////////////////////////////////////////////////////////// //C run in windows and linux if ( 0 == access(file_name, 0) ) cout<<"access(): file exist."<<endl; else cout<<"access(): file not exist."<<endl; /////////////////////////////////////////////////////////////////////// //C++ run in windows and linux fstream fs; fs.open(file_name, ios::in); if (fs) cout<<"fstream: file exist."<<endl; else cout<<"fstream: file not exist."<<endl; fs.close(); /////////////////////////////////////////////////////////////////////// //Windows API run in windows #ifdef WIN32 WIN32_FIND_DATA wfd; HANDLE hFind = FindFirstFile(file_name, &wfd); if ( INVALID_HANDLE_VALUE != hFind ) cout<<"FindFirstFile: file exist."<<endl; else cout<<"FindFirstFile: file not exist."<<endl; CloseHandle(hFind); if ( PathFileExists(file_name) ) cout<<"PathFileExists: file exist."<<endl; else cout<<"PathFileExists: file not exist."<<endl; if ( INVALID_FILE_ATTRIBUTES != GetFileAttributes(file_name) ) cout<<"GetFileAttributes: file exist."<<endl; else cout<<"GetFileAttributes: file not exist."<<endl; if ( INVALID_HANDLE_VALUE != CreateFile(file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, NULL, NULL) ) cout<<"CreateFile: file exist."<<endl; else cout<<"CreateFile: file not exist."<<endl; #endif /////////////////////////////////////////////////////////////////////// //boost run in windows and linux boost::filesystem::path path_file(file_name); if ( boost::filesystem::exists(path_file) && boost::filesystem::is_regular_file(path_file) ) cout<<"boost: file exist."<<endl; else cout<<"boost: file not exist."<<endl; return 0; }
判断文件夹代码:
////////////////////////////////////////////////////////////win32 #ifdef WIN32 #include //C (Windows) access #else #include //C (Linux) access #endif #ifdef WIN32 #include //Windows API FindFirstFile #include #pragma comment(lib, "shlwapi.lib") //Windows API PathFileExists #endif ////////////////////////////////////////////////////////////boost C++ #include //boost using namespace std; //////////////////////////////////////////////////////////// int main() { char file_name[] = "C:\\迪大学院\\"; /////////////////////////////////////////////////////////////////////// //C run in windows and linux if ( 0 == access(file_name, 0) ) cout<<"access(): path exist."<<endl; else cout<<"access(): path not exist."<<endl; /////////////////////////////////////////////////////////////////////// //Windows API run in windows #ifdef WIN32 WIN32_FIND_DATA wfd; HANDLE hFind = FindFirstFile(file_name, &wfd); if ( INVALID_HANDLE_VALUE != hFind && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ) cout<<"FindFirstFile: path exist."<<endl; else cout<<"FindFirstFile: path not exist."<<endl; CloseHandle(hFind); if ( PathFileExists(file_name) ) cout<<"PathFileExists: path exist."<<endl; else cout<<"PathFileExists: path not exist."<<endl; if ( INVALID_FILE_ATTRIBUTES != GetFileAttributes(file_name) ) cout<<"GetFileAttributes: path exist."<<endl; else cout<<"GetFileAttributes: path not exist."<<endl; #endif /////////////////////////////////////////////////////////////////////// //boost run in windows and linux boost::filesystem::path path_file(file_name); if ( boost::filesystem::exists(path_file) && boost::filesystem::is_directory(path_file) ) cout<<"boost: path exist."<<endl; else cout<<"boost: path not exist."<<endl; return 0; }
比较:


评论列表