File system

File system opendir

// INCLUDE
#include <dirent.h>

// PROTOTYPE
DIR* opendir(const char* pathname)

Open a directory stream to iterate the files in the directory. Be sure to close the directory when done using closedir. Do not attempt to free the returned DIR*, only use closedir. You should closedir as soon as possible. Do not leave directory handles open for later use; instead close an reopen the directory later.

  • pathname: The pathname to the directory (Unix-style, with forward slash as the directory separator).

Returns NULL (0) on error, or a non-zero value for use with readdir.

Note:

You must not call unlink() with an open directory handle to one or more parent directories! The common case where this can occur is if using the combination of opendir() and readdir(), and attempting to remove a file from within the loop. Doing so can cause an error, or, in some cases, corrupt the file system.

You should either save a list of all files to delete then unlink them after closedir, or use findLeafEntry(). For examples of the latter, see the SequentialFileRK library, or the rmrf helper in Device OS.