ファイルがフォルダかどうかを確認しようとしていますが、この行を変更すると次のようになります。
snprintf(buf, sizeof buf, "%s\\%s", path, e->d_name);
^ ^
+------+ note the differences
これに:
snprintf(buf, sizeof buf, "%s\b%s", d->dd_name, e->d_name);
^ ^
+------+ note the differences
失敗するため、「isfolder」または「isfile」は出力されませんstat(...)
。両方のラインが同じ出力パスを生成しますが。
どうしたの?
コード:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
int main(int argc, char** argv) {
DIR *d;
struct dirent *e;
struct stat fs;
char*path = "C:\\Users\\Me\\Documents\\NetBeansProjects\\MyApp";
d = opendir(path);
if (d != NULL) {
while (e = readdir(d)) {
char buf[256];
snprintf(buf, sizeof buf, "%s\\%s", path, e->d_name); // <- here
printf("%s\n",buf);
if (stat(buf, &fs) < 0) continue;
if (S_ISDIR(fs.st_mode)) {
printf("is folder");
} else {
printf("is file");
}
}
closedir(d);
}
return 0;
}