How can I determine the list of files in a directory from inside my C or C++ code?
I\'m not allowed to execute the ls
command and parse the results from
For a C only solution, please check this out. It only requires an extra header:
https://github.com/cxong/tinydir
tinydir_dir dir;
tinydir_open(&dir, "/path/to/dir");
while (dir.has_next)
{
tinydir_file file;
tinydir_readfile(&dir, &file);
printf("%s", file.name);
if (file.is_dir)
{
printf("/");
}
printf("\n");
tinydir_next(&dir);
}
tinydir_close(&dir);
Some advantages over other options:
readdir_r
where available, which means it's (usually) threadsafeUNICODE
macros