Search code examples
c++c++17std-filesystem

How to remove quotation marks from std::filesystem::path


If I use functions like absolute() I always get a path which contains quotation marks.

Is there a way within the filesystem functions to remove this quotation marks which enables it to use with e.g. std::ifstream?

  fs::path p2 { "./test/hallo.txt" };
  std::cout << "absolte to file : " << fs::absolute(p2) << std::endl;

returns:

"/home/bla/blub/./test/hallo.txt"

I need

/home/bla/blub/./test/hallo.txt

instead.

It is no problem to do it manually, but I want to ask if there is a method inside the filesystem lib.


Solution

  • std::operator << (std::filesystem::path const& p) is specified as follows:

    Performs stream input or output on the path p. std::quoted is used so that spaces do not cause truncation when later read by stream input operator.

    So this is expected behaviour when streaming a path. What you need is path::string():

    Returns the internal pathname in native pathname format, converted to specific string type.

    std::cout << "absolute to file : " << absolute(p2).string() << std::endl;
    //                                               ^^^^^^^^^
    

    I've also removed fs:: since absolute can be found via ADL.