std :: filesystem :: canonical和std :: filesystem :: absolute无法正常工作

时间:2020-07-14 17:31:39

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

我正在使用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传递到该文件夹​​并运行命令,这意味着enter image description here

输出为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: 

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



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

我也尝试过绝对,它也给了我相同的输出。


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

enter image description here

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代码没有任何输出。

1 个答案:

答案 0 :(得分:0)

我使用了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
相关问题