【发布时间】:2011-04-01 23:53:11
【问题描述】:
程序是打开一个目录并显示文件名... 即如果有一个文件..它应该说 FILE..else DIRECTORY.. 但是程序将所有文件显示为目录..
有人可以检查代码是否有任何错误....thnx
#include<stdio.h>
#include<dirent.h>
#define DIR_path "root/test"
main()
{
DIR *dir;
dir=opendir(DIR_PATH);
printf("THe files inside the directory :: \n");
struct dirent *dent;
if(dir!=NULL)
{
while((dent=readdir(dir)))
{
FILE *ptr;
printf(dent->d_name);
if(ptr=fopen(dent->d_name,"r"))
{
print("\tFILE\n");
fclose(ptr);
}
else
printf("\t DIRECTORY\n");
}
close(dir);
}
else
printf("ERROR OPENIN DIRECTORY");
}
【问题讨论】:
-
stackoverflow.com/questions/1542763/…、stackoverflow.com/questions/3029633/… 和/或stackoverflow.com/questions/1271064/… 的可能欺骗。顺便说一句,这是我见过的最丑陋的 dirent 用法。查看链接了解原因。
标签: c file directory opendir dirent.h