How can I list directories using a similar function like opendir, but using the filesystem library from C++?
The opendir function from dirent, opens the directory but we can't see it, which is okay.
for (const auto & entry : fs::recursive_directory_iterator(dir))
Basically, this is the code I use to loop through the directories. fs is the filesystem.
Now, the opendir actually opens the directory silently. Now if it doesn't have enough permissions, it can't open the directory and it will return, No such directory. (I didn't write this function myself)
void SearchFiles(std::string Directory)
{
DIR* pDir;
if((pDir = opendir(Directory.c_str())) != NULL)
{
struct dirent* pEntry;
/* print all the files and directories within directory */
while((pEntry = readdir(pDir)) != NULL)
{
if(pEntry->d_type == DT_DIR)
{
std::string Name = pEntry->d_name;
if(Name != "." && Name != "..")
SearchFiles(Directory + Name + '\\');
}
else if(pEntry->d_type == DT_REG)
{
g_vFiles.push_back(Directory + pEntry->d_name);
}
}
closedir(pDir);
}
else
{
printf("No such directory: '%s'\n", Directory.c_str());
}
}
Now, I don't understand the code above a lot, but yeah...
Not sure how new the filesystem library is, but does it have a function or something so I can make that what it does above?
Because when using my method with the filesystem code above, it lists everything even those that I do not have permissions to, I guess.
Incase that doesn't exist without using the dirent.h one. Do I really have to use the * to do the pDir thing? Can't I just write DIR pDir, because the pointer thing is kinda not really clear to me.