There are standard library functions and classes to read and write data to files. But before C++17, there were no functions to list files in a directory, get the type of a file, or get access rights for a file.
Let's see how such iniquities can be fixed using Boost. We'll be doing a program that lists names, write accesses, and types of files in the current directory.
Some basics of C++ would be more than enough for using this recipe.
This recipe requires linking against the boost_system
and boost_filesystem
libraries.
This recipe and the next one are about portable wrappers for working with a filesystem:
- We need to include the following two headers:
#include <boost/filesystem/operations.hpp> #include <iostream>
- Now, we need to specify a directory:
int main() { boost::filesystem::directory_iterator begin("./");
- After specifying the directory, loop through its content:
boost::filesystem::directory_iterator end; for...