このコードでは、ディレクトリを開き、リストが通常のファイルではない(フォルダであることを意味する)かどうかをチェックします。 C++でファイルとフォルダを区別するにはどうしたらいいですか?この場合に役立ちます はここに私のコードです: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;
}
使用 'stat'(または' lstat')とは異なる場合は、元のディレクトリとファイル名にパッチを適用する必要があると思いますし、 'S_ISDIR'。 –