13

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.

Rakete1111
  • 47,013
  • 16
  • 123
  • 162
Klaus
  • 24,205
  • 7
  • 58
  • 113
  • Which platform? – John Zwinck Apr 27 '17 at 13:44
  • 1
    @JohnZwinck I thought the idea behind using std::filesystem is, that I remove platform dependency :-) I currently use linux, but code must be portable. – Klaus Apr 27 '17 at 13:45
  • [How to get full path by just giving filename?](http://stackoverflow.com/questions/27247991/how-to-get-full-path-by-just-giving-filename) – Khalil Khalaf Apr 27 '17 at 13:45
  • 3
    Congratulations! You may be the first person to find a use for [`std::quoted`](http://en.cppreference.com/w/cpp/io/manip/quoted). – nwp Apr 27 '17 at 13:45
  • 2
    If you think platform independence is possible on a filesystem you are in for a bad surprise! – John Zwinck Apr 27 '17 at 13:45
  • 2
    I might be missing something but... you do realize that it's the `operator<<` overload for `boost::filesystem::path` that emits the double quotes? – G.M. Apr 27 '17 at 13:50
  • @G.M.: No! Thanks for that hint. As also given by answer from Quentin path.string() gives the correct result. Thanks! – Klaus Apr 27 '17 at 13:53

1 Answers1

25

std::operator << (std::filesystem::path const &) 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 << "absolte to file : " << absolute(p2).string() << std::endl;
//                                               ^^^^^^^^^

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

Quentin
  • 62,093
  • 7
  • 131
  • 191