C++ – How to know the type of a file using Boost.Filesystem


I'm using Boost but I cannot find complete (or good) documentation about the filesystem library in the installation directory nor the web. The "-ls" example I found has been quite a helper but it's not enough.

Thanks in advance 🙂

Best Solution

Here is an example:

#include <iostream>
#include <boost/filesystem.hpp>
#include <string>

using namespace std;

int main() {
    string filename = "hello.txt";

    string extension = boost::filesystem::extension(filename);

    cout << "filename extension: " << extension << endl;

    return 0;    

The output is ".txt"

Reminder: Compile with '-lboost_system -lboost_filesystem'