标签: boost-filesystem

如何从另一条路径中减去一条路径?

所以......我有一个基本路径和一个新路径.新路径包含基本路径.我需要看看新路径有什么不同.就像我们/ home /和新路径是/ home/apple/one一样,我需要从它获得apple/one.注意 - 当我从(homePath/diffPath)创建一些路径时,我需要再次获得/ home/apple/one.如何用Boost FileSystem做这样的事情?

c++ boost boost-filesystem

4
推荐指数
1
解决办法
1798
查看次数

boost :: filesystem :: directory_iterator是否因删除而失效?

我正在遍历一个目录,当一个项目符合某些条件时,我将其删除.我可以在循环内安全地完成它,或者我必须在数组中保存路径并稍后删除吗?

我没有在boost :: filesystem docs中找到相关信息.

iterator boost-filesystem

4
推荐指数
1
解决办法
1338
查看次数

如何使用Boost库更改文件权限?

如何使用Boost库将文件的权限更改为只读?

我已经看过一些问题,比如这个这个,但我还是不知道该怎么做,我试过做

boost::filesystem::wpath path = L"abc.txt";
if( boost::filesystem::exists( path ) && boost::filesystem::is_regular_file( path ) )
{
    boost::filesystem::file_status s = boost::filesystem::status( path );
    /* here I need to set file permissitons to READ ONLY for `path` file */
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

c++ permissions boost boost-filesystem

4
推荐指数
2
解决办法
1万
查看次数

使用boost :: flilesystem提取目录的父文件夹

假设我有以下文件夹

std::string m("C:\MyFolderA\MyFolderB\MyFolderC");
boost::filesystem::path p(m);
Run Code Online (Sandbox Code Playgroud)

无论如何,我有没有要提取此文件夹的父文件夹。我想MyFolderB.从上面的路径中获取字符串。

c++ boost-filesystem

4
推荐指数
1
解决办法
6844
查看次数

parent_path()带或不带斜杠

文档中所述,以下预期输出为:

boost::filesystem::path filePath1 = "/home/user/";
cout << filePath1.parent_path() << endl; // outputs "/home/user"

boost::filesystem::path filePath2 = "/home/user";
cout << filePath2.parent_path() << endl; // outputs "/home"
Run Code Online (Sandbox Code Playgroud)

问题是,你如何处理这个问题?也就是说,如果我接受一个路径作为参数,我不希望用户关心它是否应该有一个尾部斜杠.看起来最简单的事情就是附加一个尾部斜杠,然后调用parent_path()TWICE来获取我想要的"/ home"的父路径:

boost::filesystem::path filePath1 = "/home/user/";
filePath1 /= "/";
cout << filePath1.parent_path().parent_path() << endl; // outputs "/home"

boost::filesystem::path filePath2 = "/home/user";
filePath2 /= "/";
cout << filePath2.parent_path().parent_path() << endl; // outputs "/home"
Run Code Online (Sandbox Code Playgroud)

但这看起来很荒谬.在框架内有更好的方法来处理这个问题吗?

c++ boost boost-filesystem

4
推荐指数
2
解决办法
2990
查看次数

将辅助扩展添加到 boost::filesystem::path 的正确方法是什么?

我想向路径附加附加扩展名:

namespace fs = boost::filesystem;
fs::path append_extension(const fs::path& path, const fs::path& ext);
Run Code Online (Sandbox Code Playgroud)

预期行为:

  • append_extension("foo.txt", ".log") -> "foo.txt.log"
  • append_extension("foo.txt", "log") -> "foo.txt.log"
  • append_extension("foo", "log") -> "foo.log"

append_extension是否可以在不使用点字符进行字符串操作的情况下实现?

c++ boost boost-filesystem

4
推荐指数
1
解决办法
3698
查看次数

这是boost :: filesystem中的错误吗?为什么boost :: filesystem :: path :: string()在Windows和Linux上没有相同的签名?

我想要的向量转换boost::filesystem::pathstd::string使用成员函数string().我写了这个,它在Windows上工作正常(MSVC 14,2015):

std::transform(
    users.begin(), users.end(), std::back_inserter(usersStrs),
    std::mem_fn(static_cast<const std::string (PathType::*)() const>(
        &PathType::string)));
Run Code Online (Sandbox Code Playgroud)

现在我转到gcc(6.3,Debian Stretch),我的代码给出了上面签名不存在的链接错误.要修复它,我不得不将代码更改为:

std::transform(
    users.begin(), users.end(), std::back_inserter(usersStrs),
    std::mem_fn(static_cast<const std::string& (PathType::*)() const>(
        &PathType::string)))
Run Code Online (Sandbox Code Playgroud)

PS:我知道lambda解决方案更容易,我现在切换到了必需的解决方案.

起初,我认为MSVC更宽容,但后来我切换回Windows并得到相反的链接错误,第一个签名是正确的.我去了源代码(1.64,path.hpp),这就是我发现的:

#   ifdef BOOST_WINDOWS_API
    const std::string string() const
    {
      std::string tmp;
      if (!m_pathname.empty())
        path_traits::convert(&*m_pathname.begin(), &*m_pathname.begin()+m_pathname.size(),
        tmp);
      return tmp;
    }
//...
#   else   // BOOST_POSIX_API
    //  string_type is std::string, so there is no conversion
    const std::string&  string() const { return m_pathname; }
//...
#   endif
Run Code Online (Sandbox Code Playgroud)

所以我看到的原因是在Windows上,因为它默认不使用UTF-8,所以有一个临时转换.但为什么不提升Windows和Linux使用相同的API?最坏的情况是,它会花费一个字符串的副本.对?

有没有替代path::string()我应该使用的跨平台API稳定性?

c++ linux boost boost-filesystem c++11

4
推荐指数
1
解决办法
672
查看次数

如何将文件保存到可能的新目录中?

所以我有一些基础boost::filesystem::path Base我想创建文件夹,如果不存在,并从字符串创建二进制文件.目前我有这样的功能:

void file_service::save_string_into_file( std::string contents, std::string name )
{
    std::ofstream datFile;
    name = "./basePath/extraPath/" + name;
    datFile.open(name.c_str(), std::ofstream::binary | std::ofstream::trunc | std::ofstream::out    );
    datFile.write(contents.c_str(), contents.length());
    datFile.close();
}
Run Code Online (Sandbox Code Playgroud)

它要求从目录中存在.所以我想知道如何更新我的函数boost.filesystem API以达到所需的功能?

c++ boost file save boost-filesystem

3
推荐指数
1
解决办法
4860
查看次数

在XCode 3.2中链接Boost文件系统(MacPorts)

我正在使用XCode 3.2并使用MacPorts项目(发布和调试)安装了boost但是我无法让我的应用程序与boost :: filesystem链接

我已将库搜索路径设置为/ opt/local/lib,但在链接项目时仍然会得到未解析的引用.

我注意到在另一篇文章中将-lboost_system添加到"其他链接器标志"但是当我这样做时,我得到了 - libboost_system.dylib,文件是为不支持的文件格式构建的,而不是被链接的体系结构(i386).

MacPorts没有安装正确的库还是我错过了其他的东西?

马丁

macos xcode boost macports boost-filesystem

3
推荐指数
1
解决办法
2283
查看次数

使用具有目录迭代器范围的boost范围适配器

编辑:根据Jonathan的回答,我在我的问题下面添加了一些解决方案

我想在给定目录中有一个具有特定名称模式的常规文件列表.我从boost.filesystem(boost 1.53)中获取了一个示例并对其进行了修改.这是我基本上想要的工作版本:

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

using namespace std;
using namespace boost::filesystem;

int main(int argc, char* argv[])
{
  path p ("."); // search directory

  try
  {
    if (exists(p))
    {
      if (is_directory(p))
      {
        cout << "logfiles in " << absolute(p) << '\n';
      // I want to replace this part:
        std::list<directory_entry> datFiles;
        for(directory_iterator it(p); it != directory_iterator(); ++it)
        {
          if (is_regular_file(*it) && (extension(*it) == ".dat"))
          {
            datFiles.push_back(*it);
          }
        }
      // part to be replaced ends …
Run Code Online (Sandbox Code Playgroud)

c++ filter boost-filesystem boost-range

3
推荐指数
1
解决办法
4294
查看次数