Tôi có mã này mở thư mục và kiểm tra xem danh sách không phải là tệp thông thường (có nghĩa là nó là một thư mục) nó cũng sẽ mở nó. Làm thế nào tôi có thể phân biệt giữa các tập tin và thư mục với C + +. đây là mã của tôi nếu điều này giúp:Phân biệt giữa các thư mục và tệp trong C++
#include <sys/stat.h>
#include <cstdlib>
#include <iostream>
#include <dirent.h>
using namespace std;
int main(int argc, char** argv) {
// Pointer to a directory
DIR *pdir = NULL;
pdir = opendir(".");
struct dirent *pent = NULL;
if(pdir == NULL){
cout<<" pdir wasn't initialized properly!";
exit(8);
}
while (pent = readdir(pdir)){ // While there is still something to read
if(pent == NULL){
cout<<" pdir wasn't initialized properly!";
exit(8);
}
cout<< pent->d_name << endl;
}
return 0;
}
Sử dụng 'stat' (hoặc 'lstat') và 'S_ISDIR'. –