Solution for Exercise 23-2
ex2302
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
int main()
{
DIR *folder;
struct dirent *file;
folder=opendir(".");
if(folder==NULL)
{
puts("Unable to read directory");
exit(1);
}
while(file = readdir(folder))
printf("Found the file '%s'\n",file->d_name);
closedir(folder);
return(0);
}
Notes
* As long as the readdir() function returns a pointer to a dirent structure (Line 16), the while loop spins and directory entries are read.
* Feel free to ignore any "suggest parentheses" warning about Line 16. The compiler sees the single = sign and assumes that you meant to specify two, such as == for "is equal to." In the context as presented in this code, the statement is correct. That's why the compiler issued a warning and not an error.
Copyright © 1997-2025 by QPBC.
All rights reserved
