-2

我正在使用 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 代码的输出。

4

1 回答 1

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
于 2020-07-14T17:48:10.240 回答