判断文件、目录是否存在:C、C++、Windows API、 boost
2013-12-11 10:15
260 查看
一、判断文件是否存在
二、判断目录是否存在
三、几种方式比较
1. 精确判断文件和目录的
FindFirstFile() (Windows API, Windows)
boost (boost, Windows and Linux)
2. 不精确判断文件盒目录的
access() (C , Windows and Linux)
PathFileExists() (Windows API , Windows)
GetFileAttributes() (Windows API, Windows)
3. 只能判断文件的
fstream (C++ STL, Windows and Linux)
CreateFile() (Windows API, Windows)
#ifdef WIN32 #include <io.h> //C (Windows) access #else #include <unistd.h> //C (Linux) access #endif #include <fstream> //C++ fstream #ifdef WIN32 #include <Windows.h> //Windows API FindFirstFile #include <Shlwapi.h> #pragma comment(lib, "shlwapi.lib") //Windows API PathFileExists #endif #include <boost/filesystem.hpp> //boost using namespace std; int main() { char file_name[] = "D://aa.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; }
二、判断目录是否存在
#ifdef WIN32 #include <io.h> //C (Windows) access #else #include <unistd.h> //C (Linux) access #endif #ifdef WIN32 #include <Windows.h> //Windows API FindFirstFile #include <Shlwapi.h> #pragma comment(lib, "shlwapi.lib") //Windows API PathFileExists #endif #include <boost/filesystem.hpp> //boost using namespace std; int main() { char file_name[] = "D://b"; //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; }
三、几种方式比较
1. 精确判断文件和目录的
FindFirstFile() (Windows API, Windows)
boost (boost, Windows and Linux)
2. 不精确判断文件盒目录的
access() (C , Windows and Linux)
PathFileExists() (Windows API , Windows)
GetFileAttributes() (Windows API, Windows)
3. 只能判断文件的
fstream (C++ STL, Windows and Linux)
CreateFile() (Windows API, Windows)
相关文章推荐
- 判断文件、目录是否存在:C、C++、Windows API、 boost
- Linux - 判断文件/目录是否存在/具有权限
- linux+笔记:linux下判断一个文件或者目录是否存在
- C/C++中判断某一文件或目录是否存在&lt;转&gt;
- linux下判断文件和目录是否存在的方法(总结)
- 判断文件和目录是否存在
- linux下python脚本判断目录和文件是否存在
- shell判断文件,目录是否存在或者具有权限
- [IO]判断文件或者目录是否存在
- Shell脚本中判断文件,目录是否存在或者具有权限
- shell 判断文件、目录是否存在
- Go语言编程中判断文件是否存在是创建目录的方法
- shell小技巧实战-判断文件或目录是否存在
- C/C++中判断某一文件或目录是否存在
- shell判断文件,目录是否存在或者具有权限
- PHP如何判断文件、目录是否存在?
- PHP file操作类 判断文件或目录是否存在 复制删除文件等
- Lua判断一个目录或文件是否存在的方法
- shell判断文件,目录是否存在或者具有权限
- C/C++中判断某一文件或目录是否存在