C ++文件系统

时间:2016-02-29 07:52:19

标签: c++ c++11 boost filesystems

我正在尝试添加这些功能。

std::string GetDirectory(std::string path_name) {
    boost::filesystem::path path(path_name.c_str());

    path_name = path.string();

    path_name.erase(
        path_name.substr(
            path_name.find_last_of(DIRECTORY_SEPARATOR),
            path_name.length()
        )
    );
    return path_name;
}

std::string GetName(std::string path_name) {
    boost::filesystem::path path(path_name.c_str());

    path_name = path.string();

    path_name.erase(
        path_name.substr(
            0, 
            path_name.find_last_of(DIRECTORY_SEPARATOR)
        )
    );

    path_name.erase(
        path_name.substr(
            path_name.find_last_of("."), 
            path_name.length()
        )
    );
    return path_name;
}

std::string GetExtension(std::string path_name) {
    boost::filesystem::path path(path_name.c_str());

    path_name = path.string();

    path_name.erase(
        path_name.substr(
            0, 
            path_name.find_last_of('.')
        )
    );
    return path_name;
}

我想补充一下:

  

C:\的Windows \用户\实施例\桌面\ test.txt的

GetDirectory

  

C:\的Windows \用户\实施例\桌面

的GetName

  

测试

GetExtension

  

TXT

我在path_name和erase(句点)之间遇到错误。 非常感谢帮助,谢谢。

请索取错误消息:

  

1> C:\ Program Files(x86)\ MSBuild \ Microsoft.Cpp \ v4.0 \ V140 \ Microsoft.CppBuild.targets(368,5):警告MSB8004:输出目录不以尾部斜杠结尾。此构建实例将添加斜杠,因为它允许正确评估输出目录。   1 GT; main.cpp中   1> main.cpp(287):错误C2664:'std :: _ St​​ring_iterator>> std :: basic_string,std :: allocator> :: erase(std :: _ St​​ring_const_iterator>>,std :: _ St​​ring_const_iterator>>)':无法从'std :: basic_string,std :: allocator>'转换参数1 'unsigned int'   1 GT; main.cpp(287):注意:没有可用于执行此转换的用户定义转换运算符,或者无法调用运算符   1 GT; main.cpp(301):错误C2664:'std :: _ St​​ring_iterator>> std :: basic_string,std :: allocator> :: erase(std :: _ St​​ring_const_iterator>>,std :: _ St​​ring_const_iterator>>)':无法从'std :: basic_string,std :: allocator>'转换参数1 'unsigned int'   1 GT; main.cpp(301):注意:没有可用于执行此转换的用户定义转换运算符,或者无法调用运算符   1 GT; main.cpp(307):错误C2664:'std :: _ St​​ring_iterator>> std :: basic_string,std :: allocator> :: erase(std :: _ St​​ring_const_iterator>>,std :: _ St​​ring_const_iterator>>)':无法从'std :: basic_string,std :: allocator>'转换参数1 'unsigned int'   1 GT; main.cpp(307):注意:没有可用于执行此转换的用户定义转换运算符,或者无法调用运算符   1 GT; main.cpp(321):错误C2664:'std :: _ St​​ring_iterator>> std :: basic_string,std :: allocator> :: erase(std :: _ St​​ring_const_iterator>>,std :: _ St​​ring_const_iterator>>)':无法从'std :: basic_string,std :: allocator>'转换参数1 'unsigned int'   1 GT; main.cpp(321):注意:没有可用于执行此转换的用户定义转换运算符,或者无法调用运算符

2 个答案:

答案 0 :(得分:1)

你应该使用

template <class Path> typename Path::string_type extension(const Path& p);

获取扩展名的功能。

你应该使用

template <class Path> typename Path::string_type basename(const Path& p);

用于获取文件的基本名称。

我假设您使用的是Windows操作系统。

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

int main()
{
    namespace fs = boost::filesystem;

    std::string p = "C:\\Windows\\Users\\Example\\Desktop\\test.txt";

    std::string ext = fs::extension(p);
    std::string bas = fs::basename(p);

    std::cout << ext << " " << bas << std::endl;

    return 0;
}

答案 1 :(得分:-1)

  • 删除无用且有害的path_name.substr()
  • 您可以省略std::string::erase的第二个参数,直到字符串结束为止。