Questions tagged [std-filesystem]

c++ std::filesystem discussion, std::filesystem was added to the ISO C++ standard library in C++17

Specification available from http://en.cppreference.com/w/cpp/filesystem

165 questions
8
votes
1 answer

Clang refuses to compile libstdc++'s header

Consider this minimal example: #include #include int main() { std::cout << std::filesystem::current_path() << '\n'; } It works as expected in GCC 9.2, but Clang 8.0.1 refuses to compile the header (from GCC…
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
8
votes
1 answer

Get executable's path (with std::filesystem)

Before i get marked as duplicate, all similar questions i could find have answers prior to the introduction of std::filesystem, and either use platform-specific code or Boost::filesystem. I'm looking for a portable answer that uses…
Barnack
  • 921
  • 1
  • 8
  • 20
7
votes
2 answers

Check if directory exists using

I have a string that contains the path to some file. The file doesn't need to exist (in my function it can be created), but it's necessary that directory must exist. So I want to check it using the library. I tried this…
Dark_Phoenix
  • 368
  • 3
  • 14
7
votes
1 answer

Program crashes when filesystem::path is destroyed

The following program crashes: #include #include namespace fs = std::filesystem; int main() { fs::path p1 = "/usr/lib/sendmail.cf"; std::cout << "p1 = " << p1 << '\n'; } Compilation: $ g++ -std=c++17…
Turtle10000
  • 213
  • 1
  • 13
7
votes
1 answer

Check if an std::filesystem::path is inside a directory

I have a root path represented by an std::filesystem::path. I want to add some user provided filename to this path and make sure the resulting path is not out of the root directory. For example: std::filesystem::path root = "/foo/bar"; …
Nicolas Appriou
  • 2,208
  • 19
  • 32
7
votes
1 answer

Does use of C++17 std::filesystem REQUIRE MacOS 10.15? (Xcode 11.1)

Using Xcode 11.1, building on MacOS 10.14.6 (Mojave), the following lines: #include typedef std::filesystem::path my_path; ...generate this compiler error: 'path' is unavailable: introduced in macOS 10.15 Does this mean I can't build…
SMGreenfield
  • 1,680
  • 19
  • 35
7
votes
2 answers

Cross-platform way to handle std::string/std::wstring with std::filesystem::path

I have a sample piece of C++ code that is throwing an exception on Linux: namespace fs = std::filesystem; const fs::path pathDir(L"/var/media"); const fs::path pathMedia = pathDir / L"COMPACTO - Diogo Poças.mxf" // <-- Exception thrown here The…
ZeroDefect
  • 663
  • 1
  • 8
  • 27
6
votes
3 answers

Version of std::filesystem::equivalent for non-existing files

My program is supposed to create two files with user-specified paths. I need to know if the paths lead to the same location, to end with an error before I start changing the filesystem. Because the paths come from the user, they are expected to be…
Piotr Siupa
  • 3,929
  • 2
  • 29
  • 65
6
votes
2 answers

`std::filesystem::directory_iterator` graceful handling of non-existing dir

I have the following code: for (const auto& x : std::filesystem::directory_iterator(dir)) { // do stuff with x } dir might not exist, and I want to treat this case as if the dir is empty. I can't seem to come up with a nice option. If I guard…
Paul
  • 6,061
  • 6
  • 39
  • 70
6
votes
1 answer

std::filesystem recursive iterator will throw permission_denied even when skip_permission_denied is in use

I am writing a linux command line program that will return the size of a directory. The program works as expected, except when specifically dealing with root directories. I know many files in the root directory do not have sizes because they are…
Lucas Streanga
  • 469
  • 2
  • 7
6
votes
2 answers

Get the last file name from std::filesystem::directory_iterator() without iteration?

I am trying to get the last file name. Below code does it very elegantly. But it is iterating through all the files inside the directory. Is it possible to get the last file without iteration? #include std::string latestFileName; for…
Jai
  • 377
  • 2
  • 14
6
votes
2 answers

Implicit conversion between std::filesystem::path and std::string, should it happen?

The cppreference page for std::filesystem::path states: Paths are implicitly convertible to and from std::basic_strings, which makes it possible to use them with over files APIs, e.g. as an argument to std::ifstream::open Now the conversion to a…
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
6
votes
2 answers

What is the difference between std::filesystem::copy() and std::filesystem::copy_file()?

What is the difference between std::filesystem::copy() and std::filesystem::copy_file() in this code? #include void testing() { const std::filesystem::path src = "foo.txt"; const std::filesystem::path dst = "bar.txt"; …
Stéphane
  • 19,459
  • 24
  • 95
  • 136
6
votes
1 answer

How should I make std::filesystem appear standards-conforming to Visual Studio 2015

I have a project that is currently locked into Visual Studio 2015. However, I want to write code that is as standards-conforming as possible. I want to use std::filesystem but it didn't make it into the standard until C++-17. Fortunately, just…
jwm
  • 1,504
  • 1
  • 14
  • 29
5
votes
0 answers

How do I use std::filesystem to see if I have write access?

I want to use std::filesystem to query about a disk folder path that was given to my function. I want to find out if I have write access to the folder. But I want to do it without actually trying to write to the folder. This last requirement is…
Joe
  • 5,394
  • 3
  • 23
  • 54
1
2
3
10 11