判断是文件还是文件夹。
- #ifdef WIN32
- #include <windows.h>
- #include <direct.h>
- #else
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <dirent.h>
- #endif
- /**
- * @brief isFileOrDir
- *
- * Detailed description.
- * @param[in] path
- * @return int 0:不存在,1:为文件夹, -1:为文件
- */
- inline int isFileOrDir(std::string path)
- {
- #ifdef WIN32
- DWORD dwAttr = GetFileAttributes(path.c_str());
- //Not exist
- if (dwAttr == 0xFFFFFFFF)
- {
- return 0;
- }
- //exist
- else if (dwAttr & FILE_ATTRIBUTE_DIRECTORY)
- {
- return 1;
- }
- //is File
- else
- {
- return -1;
- }
- #else
- if (0 == access(path.c_str(), 0))
- {
- struct stat *buf;
- buf = (struct stat *) malloc(sizeof(struct stat));
- memset(buf, 0, sizeof(struct stat));
- stat(path.c_str(), buf);
- if (S_ISDIR(buf->st_mode))
- {
- free(buf);
- buf=NULL;
- return 1;
- }
- else
- {
- free(buf);
- buf=NULL;
- return -1;
- }
- }
- else
- {
- return 0;
- }
- #endif
- }
关键词:文件 文件夹 判断 是否为文件 是否为文件夹 文件是否存在 文件夹是否存在 跨平台 Linux Windows
Key Words: File, Directory, Exists, Cross-platform, Linux, Windows