Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

boost

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 :)

like image 448
reefaktor Avatar asked Jun 06 '09 14:06

reefaktor


3 Answers

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'

like image 111
uol3c Avatar answered Sep 18 '22 07:09

uol3c


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

like image 26
Laserallan Avatar answered Sep 18 '22 07:09

Laserallan


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

like image 42
serup Avatar answered Sep 22 '22 07:09

serup