如何使用Boost.Filesystem知道文件的类型?

ree*_*tor 9 c++ boost

我正在使用Boost但是我找不到关于安装目录和web中的文件系统库的完整(或好)文档.我找到的"-ls"示例是一个很好的帮手,但这还不够.

提前致谢 :)

Jee*_*lee 13

这是一个例子:

#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;    
}
Run Code Online (Sandbox Code Playgroud)

输出为".txt"

提醒:使用'-lboost_system -lboost_filesystem'编译


Las*_*lan 5

怎么样:

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

可以在此子页面上找到用于确定文件类型(目录,普通文件等)的函数:http://www.boost.org/doc/libs/1_39_0/libs/filesystem/doc/reference.html#file_status

如果您正在寻找文件扩展名,请查看:template <class Path> typename Path::string_type extension(const Path &p);页面上:http: //www.boost.org/doc/libs/1_39_0/libs/filesystem/doc/reference.html#Convenience-functions


小智 4

以下是如何从文件中获取扩展名的示例:

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();
Run Code Online (Sandbox Code Playgroud)

此示例仅获取所有文件并从中去除扩展名并显示在标准输出上,您可以修改函数 GetAllFileExtensions 以仅查看一个文件