gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:11:55 24 4
gpt4 key购买 nike

我想将目录中文件的路径保存为字符串。来自 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com