1

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.

karl-police
  • 983
  • 8
  • 21
  • How is the pointer not clear to you? The pointer is returned by `opendir`. Failure is apparently returning `nullptr`. You could always make a new ref variable as follows: `DIR &refDir = *pDir;` after checking for `nullptr` of course. But using the pointer is perfectly valid. – Neijwiert Apr 17 '19 at 09:32
  • Wait what exactly is the issue with the recursive iterator? Is it that it will list files you can't access, is that what you wish to avoid? – Qubit Apr 17 '19 at 09:34
  • All I know from the pointer is that there are stuff like & and *. * turns something into a memory address that is pointed to DIR, apperantly. And yeah... – karl-police Apr 17 '19 at 09:35
  • @Qubit I want to avoid that it accesses folders and files I can't access. Also, what opendir does that filesystem doesn't do is, that it doesn put a \ behind each directory, I guess that's the open thing. Also, my method with listing everything and pushing it into an vector array, takes too long, while the other program steps a head to the next step. What I've noticed is, if the program doesn't notices a ".", in the folder or whatever name, it will push it inside the file array, and well, for everything else it tries to put a \ behind it.... to see if that works? – karl-police Apr 17 '19 at 09:38
  • Well you could always write something similar yourself using the filesystem library. I suppose you avoid using the recursive iterator directly and instead use the regular one. Then you check each file, if it is a directory, you call your function recursively, much like in your example above. While doing so, you can also check for permissions, so if you don't have permission, simply skip. Although I would imagine that worked by default unless you run your code as the administrator of the system. – Qubit Apr 17 '19 at 09:40
  • After looking at the reference a bit, you might be interested in trying `directory_options`: https://en.cppreference.com/w/cpp/experimental/fs/directory_options – Qubit Apr 17 '19 at 09:42
  • @Qubit The original program when run as administrator, searching through C:\ well, basically only SYSTEM can access it. I don't want my program to search the content of files like pagefile.sys... I mean... does it really have to open the file silently to search through it? The other thing the program does is using an external library called mman, which is for file mapping – karl-police Apr 17 '19 at 09:45
  • Well if you run something as administrator it will always have access to everything, that's sort of the point. Although Windows does seem to forget that every now and then. – Qubit Apr 17 '19 at 09:47
  • I'm new into cpp, and from other programming languages, there is a lot of stuff I've never seen before, currently trying to figure out what directory_options is, I've seen I can use options() next to the construction of the function but I haven't really figured out, only that it is an observer. – karl-police Apr 17 '19 at 09:51
  • I recommend reading [a few good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Jesper Juhl Apr 17 '19 at 11:10
  • @Qubit you know how to use it? – karl-police Apr 17 '19 at 12:18

0 Answers0