Search code examples
c++filepathc++20boost-filesystemstd-filesystem

std::filesystem::canonical and std::filesystem::absolute are not working


I am using C++2a standard Visual Studio 2019 as compiler for the code.

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;

be it canonical or absolute, it doesn't give out the output required. I don't pass the entire command as the argument, but cd till that folder and run the command, which means, enter image description here

Output with 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: 

As you can see in the image, I don't have any output of for the canonical code.

Output with 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: 

As you can see in the image, I don't have any output of for the absolute code.



Input: A file which I'm running. Basically the /. Convert it into a canonical path/absolute path.

I tried absolute too and it also gave me the same output.


Update: I tried the same code using boost filesystem, I got some output but not correct.

enter image description here

C:\dasfsa/dsfafsa/sdfsa/a.exe Which is unusable. I can manually convert that one backslash but I don't know if it's some kind of esoteric feature or a bug and if it's a bug, I don't know where else it'll change that slash and how.

Output with 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

As you can see in the image, I don't have any output of for the boost::canonical code.


Solution

  • I used boost::filesystem::absolute and it worked. I got the required output.

    code:

    #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;
    

    output:

    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