When we are developing our applications, we often need common tasks to achieve the final objective of our project.
One of the most frequent things, among others, is to list files in a directory and we generally use the internet, however, it is very simple to understand the logic and establish this concept to reduce the waste of time carrying out research.
In this quick tip we will see how to do this in a modern way with C++.
filesystem
The filesystem
library provides facilities for performing operations on file systems and their components, such as paths, regular files, and directories.
It was originally developed by Boost and in the C++ 17
version it was finally adopted by the committee to be part of the standard library (STL).
To use it we will include:
Its namespace
is very large, so it is always interesting to reduce the path by simplifying it to fs
only, and this is best done right after including the header:
If you are using a linter, it may accuse warning:
■ Namespace alias decl 'fs' is unused (fix available)
, this is because we declared it, but we haven’t used it in our code yet, but we already will!
Now let’s create a string that will store the folder/directory we want to list the files in (e.g.: /path/to/directory
), for these examples we will list the current directory:
And to list the files we will use Range-based for loop which is also affectionately confused with for-each and we go through our folder using a native filesystem
iterator which is directory_iterator()
and within the loop we will print the element that is nothing more than the ) files in the directory, using the path()
member function:
Remembering that if there is a subdirectory it will also list it.
After compiling normally and running, you will notice the files (and/or directories, if any) being listed.
If you want to make sure that the files are being listed one at a time, you can pause the display with sleep:
In this case there will be a 1 second delay in the display.
The complete code is:
The pause is commented!
Suppose you have several files like this: file1.jpg, file2.jpg,...file.10.jpg, ... file31.jpg
, when you list them they will not appear in alphabetical order.
To resolve this, in addition to respecting the order of tens, add the headers:
Create a function with an inner lambda:
And now sort the vector with sort:
This way we will solve both problems: alphabetical order and respect for tens!
Full code:
For more information visit: https://en.cppreference.com/w/cpp/filesystem.