The program is to open a directory and to display the name of the files...
i.e if there is a file..it should say FILE....else DIRECTORY..
but the program displays all the files as directory..
Could anyone pls check the code for any errors....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 ::
");
struct dirent *dent;
if(dir!=NULL)
{
while((dent=readdir(dir)))
{
FILE *ptr;
printf(dent->d_name);
if(ptr=fopen(dent->d_name,"r"))
{
print("FILE
");
fclose(ptr);
}
else
printf(" DIRECTORY
");
}
close(dir);
}
else
printf("ERROR OPENIN DIRECTORY");
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…