Solution for Exercise 23-1

ex2301

#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);
    }
    file = readdir(folder);
    printf("Found the file '%s'\n",file->d_name);
    closedir(folder);
    return(0);
}

Notes

* I thought about listing the dirent structure in the book, but it's really boring. If you want to see how it looks, examine the dirent.h file on your computer. Be aware that dirent.h may itself include other header files that could contain the actual structure definition.