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 f
Suppose that root/test contains a file called foo. The call to dent=readdir(dir) sets dent->d_name to "foo". You already have debugging output that shows this: printf(dent->d_name)¹. Then you try to open foo with fopen, but the file is actually root/test/foo. So this fails every time (unless you happen to also have a file called foo in the current directory).
There are two ways to open the right file:
Construct the full name of the file by concatenating the argument to opendir with the file name. Something like:
/*before */
size_t dir_length = strlen(DIR_PATH);
char *filename = malloc(dir_length + NAME_MAX + 2); /*error checking omitted*/
strcpy(filename, DIR_PATH);
filename[dir_length] = '/';
filename[dir_length+1] = 0;
while ((dent = readdir(dir)) != NULL) {
strcpy(filename + dir_length + 1, dent->d_name);
/*now call lstat, fopen, etc. on filename*/
Change into the directory you're listing. For example, change the opendir call to
chdir(DIR_PATH); /*error checking omitted*/
dir = opendir(".");
You have to remember to save the previous directory with getcwd and chdir back to it afterwards. This method is not recommended in production software because it is possible to have a current directory that you can't chdir back into due to permissions.
slacker has already explained why fopen can't be used to test if a file is a directory.
¹ which by the way should be puts(dent->d_name) or even better fputs(dent->d_name, stderr): your original printf call would break if a file name contains %, which is not a big problem for debugging output but is a bad habit to get into.