【问题标题】:std::filesystem::canonical and std::filesystem::absolute are not workingstd::filesystem::canonical 和 std::filesystem::absolute 不起作用
【发布时间】:2020-11-04 02:53:01
【问题描述】:

我正在使用 C++2a 标准 Visual Studio 2019 作为代码的编译器。

std::filesystem::path parentPathX(argv[0]);
std::cout << "Running: " << argv[0] << std::endl;
std::cout << "Path wise: " << parentPathX.string() << std::endl;
std::filesystem::path parentPath = std::filesystem::canonical(parentPathX.parent_path());
String parentPath1 = parentPath.string();
//findAndReplaceAll(parentPath1, " ", "^ ");
std::cout << "Parent Path: " << parentPath1 << std::endl;

无论是规范的还是绝对的,它都不会给出所需的输出。 我没有将整个命令作为参数传递,而是将 cd 传递到该文件夹​​并运行命令,这意味着

std::filesystem::canonical 输出

C:\Users\Tarun Maganti\source\repos\MWPixelAggr\Release>MWPixelAggr.exe G:\Work\abc\M-4591.tif DN wgs84 G:\Work\abc\TestMP.xlsx 3
Running: MWPixelAggr.exe
Path wise: MWPixelAggr.exe
Is Absolute? 1
Parent Path: 

正如您在图片中看到的,我没有任何规范代码的输出。

std::filesystem::absolute 输出

C:\Users\Tarun Maganti\source\repos\MWPixelAggr\Release>MWPixelAggr.exe G:\Work\abc\M-4591.tif DN wgs84 G:\Work\abc\TestMP.xlsx 3
Running: MWPixelAggr.exe
Path wise: MWPixelAggr.exe
Is Absolute? 1
Parent Path: 

正如你在图片中看到的,我没有任何绝对代码的输出。



输入: 我正在运行的文件。基本上是/。将其转换为规范路径/绝对路径。

我也尝试了 absolute,它也给了我相同的输出。


更新: 我使用 boost 文件系统尝试了相同的代码,我得到了一些输出但不正确。

C:\dasfsa/dsfafsa/sdfsa/a.exe 无法使用。我可以手动转换那个反斜杠,但我不知道它是某种深奥的功能还是错误,如果是错误,我不知道它还会在哪里改变那个斜杠以及如何改变。

boost::filesystem::canonical 输出

C:\Users\Tarun Maganti\source\repos\MWPixelAggr\Release>MWPixelAggr.exe G:\Work\abc\M-4591.tif DN wgs84 G:\Work\abc\TestMP.xlsx 3
Running: MWPixelAggr.exe
Path wise: MWPixelAggr.exe
Is Absolute? 1
Parent Path: C:/Users\Tarun Maganti\source\repos\MWPixelAggr\Release

正如您在图片中看到的,我没有任何 boost::canonical 代码的输出。

【问题讨论】:

  • 请将您的命令输出作为文本而不是图像发布
  • 在 Visual Studio 中,当前工作目录通常不是可执行文件的位置。
  • 我应该已经找到了一些路径,对吧?将文件附加到cwd 或其他东西。
  • 您在空路径上运行规范/绝对,您期望什么?
  • 不会是.当前文件夹吗?

标签: c++ filepath c++20 boost-filesystem std-filesystem


【解决方案1】:

我使用了boost::filesystem::absolute,它成功了。我得到了所需的输出。

代码:

#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
fs::path parentPathX(argv[0]);
fs::path parentPath = fs::absolute(parentPathX.parent_path());
std::cout << "Is Absolute? " << parentPath.is_absolute() << std::endl;
String parentPath1 = parentPath.string();
//findAndReplaceAll(parentPath1, "/", "\\");
std::cout << "Parent Path: " << parentPath1 << std::endl;

输出:

C:\Users\Tarun Maganti\source\repos\MWPixelAggr\Release>MWPixelAggr.exe G:\Work\abc\M-4591.tif DN wgs84 G:\Work\abc\TestMP.xlsx 3
The process cannot access the file because it is being used by another process.

C:\Users\Tarun Maganti\source\repos\MWPixelAggr\Release>MWPixelAggr.exe G:\Work\abc\M-4591.tif DN wgs84 G:\Work\abc\TestMP.xlsx 3
Is Absolute? 1
Parent Path: C:\Users\Tarun Maganti\source\repos\MWPixelAggr\Release

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-10
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-23
    相关资源
    最近更新 更多