How do I check if a directory is a file or folder?
本问题已经有最佳答案,请猛点这里访问。
好的,所以我使用mingw,direct结构没有名为d_type或stat、d_stat或dd_stat的变量。我需要知道如何使用direct结构来确定我拥有的是文件还是文件夹。这是我的密码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include <sys/types.h> #include <sys/stat.h> #include <dirent.h> #include <errno.h> #include <vector> #include <string> #include <iostream> using namespace std; /*function... might want it in some class?*/ int getdir (string dir, vector<string> &files) { DIR *dp; struct stat _buf; struct dirent *dirp; if((dp = opendir(dir.c_str())) == NULL) { cout <<"Error(" << errno <<") opening" << dir << endl; return errno; } while ((dirp = readdir(dp)) != NULL) { if(stat(dirp->d_name, &_buf) != 0x4) files.push_back(string(dirp->d_name)); } closedir(dp); return 0; } int main() { string dir = string("."); vector<string> files = vector<string>(); getdir(dir,files); for (unsigned int i = 0;i < files.size();i++) { cout << files[i] << endl; } return 0; } |
1 2 3 4 5 | boost::filesystem::is_directory() //I found it ) //So, also you can try to call stat() function. ( on Windows ) |
(_ ^ ^)