How can I know the type of a file using Boost.Filesystem?

How about:

http://www.boost.org/doc/libs/1_39_0/libs/filesystem/doc/index.htm

The functions for figuring out the file type (directory, normal file etc.) is found on this subpage: http://www.boost.org/doc/libs/1_39_0/libs/filesystem/doc/reference.html#file_status

If you are looking for the file extension check out: template <class Path> typename Path::string_type extension(const Path &p); on the page: http://www.boost.org/doc/libs/1_39_0/libs/filesystem/doc/reference.html#Convenience-functions


Here is an example of how you can fetch extension from files :

std::vector<boost::filesystem::path> GetAllFileExtensions()
{
    std::vector<boost::filesystem::path> fileExtensions;
    boost::filesystem::directory_iterator b(boost::filesystem::current_path()), e;
    for (auto i=b; i!=e; ++i)
    {
        boost::filesystem::path fe = i->path().extension();
        std::cout << fe.string() << std::endl;
        fileExtensions.push_back(fe);
    }

    return fileExtensions;
}

std::vector<boost::filesystem::path> fileExtensions = GetAllFileExtensions();

This example just takes all files and strips extension from them and shows on standard output, you could modify function GetAllFileExtensions to only look at one file


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'

Tags:

C++

Boost