c++ - 如何访问 boost 文件系统路径类的字符串表示,并删除引号

标签 c++ boost boost-filesystem

我想将目录中文件的路径保存为字符串。来自 tutorial 的示例几乎可以做我想做的事,除了它用我想删除的引号来做。现在我知道我可以通过将 .string() 添加到路径来实现,但我只是不知道在这个例子中把它放在哪里。

希望有人能帮助我。

最佳答案

正如您所说,您需要在 path 上使用 .string() 方法来输出不带引号的内容。下面修改后的教程示例输出没有引号:

#include <iostream>
#include <iterator>
#include <algorithm>
#include <boost/filesystem.hpp>
using namespace std;
using namespace boost::filesystem;

int main(int argc, char* argv[])
{
    if (argc < 2)
    {
        cout << "Usage: tut3 path\n";
        return 1;
    }

    path p(argv[1]);   // p reads clearer than argv[1] in the following code

    try
    {
        if (exists(p))    // does p actually exist?
        {
            if (is_regular_file(p))        // is p a regular file?
                cout << p.string() << " size is " << file_size(p) << '\n';

            else if (is_directory(p))      // is p a directory?
            {
                cout << p.string() << " is a directory containing:\n";

                for (directory_iterator it(p); it != directory_iterator(); ++it)
                    cout << it->path().string() << "\n";
            }
            else
                cout << p.string() << " exists, but is neither a regular file nor a directory\n";
        }
        else
            cout << p.string() << " does not exist\n";
    }

    catch (const filesystem_error& ex)
    {
        cout << ex.what() << '\n';
    }

    return 0;
}

关于c++ - 如何访问 boost 文件系统路径类的字符串表示,并删除引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31095434/

相关文章:

c++ - 如何在 VS C++ 上初始化 boost::edge_weight_t 类型

c++ - 如何结合两个 boost::filesystem::path's?

C++ - 如何以平台无关、线程安全的方式将文件的上次修改日期和时间格式化为用户首选的日期/时间区域设置格式

c++ - lex 和 yacc 中的词法分析器没有输出

c++ - C/C++ 中的大数据集表示

c++ - boost::spirit::karma 语法:带有可选属性的结构的逗号分隔输出

c++ - 为什么 Boost 原子使用中的多生产者队列是无等待的

C++ BOOST undefined reference `boost::filesystem::detail::copy_file

c++ - 如何区分ascii值和数字?

c++ - 从两个 4x64 位整数数组中取模