我正在尝试检查文件是否是文件夹,但是当我更改此行时:
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
它不会打印“是文件夹”或“是文件”,因为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;
}