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
52
votes
2 answers

VS2017: E0135 namespace "std" has no member "filesystem"

In order to use: std::filesystem from the C++17 library, my project was migrated from vs2015 to vs2017. My project compiles and runs without error, the lib is included without error, but when trying to use std::filesystem I get the following: It…
LearnMore
  • 695
  • 1
  • 5
  • 12
29
votes
2 answers

What is the C++17 equivalent to boost::filesystem::unique_path()?

std::filesystem on C++17, and std::experimental::filesystem for many pre-C++17 compilers, are based on boost::filesystem and almost all of it is obvious to port to the newer std. But I see no std::filesystem equivalent to…
Larry Gritz
  • 13,331
  • 5
  • 42
  • 42
27
votes
4 answers

Link errors using members in C++17

I'm using gcc 7.2 on Ubuntu 16.04, and I need to use the new filesystem library from C++17. Even though there is indeed a library called experimental/filesystem, I can't use any of its members. For example, when I try to compile this file: #include…
Willie Jeng
  • 323
  • 1
  • 4
  • 9
20
votes
3 answers

Native path separator bug in C++17 std::filesystem::path?

I encountered a problem when upgrading from #include to #include . It seems that the std::filesystem::path::wstring method is not returning the same string as in experimental::filesystem. I wrote the following…
Garland
  • 911
  • 7
  • 22
15
votes
1 answer

Why is it not possible to construct a `std::filesystem::path` from `std::filesystem::path` iterators?

The following piece of code aims to strip the first part of a path in case it exists: #include std::filesystem::path strip_prefix(std::filesystem::path p) { if (auto it{p.begin()}; it != p.end()) { ++it; return…
Ton van den Heuvel
  • 10,157
  • 6
  • 43
  • 82
14
votes
1 answer

How to convert `std::filesystem::file_time_type` to a string using GCC 9

I'm trying to format the modification time of a file as a string (UTC). The following code compiles with GCC 8, but not GCC 9. #include #include #include #include #include namespace fs =…
tttapa
  • 1,397
  • 12
  • 26
13
votes
1 answer

How to get the last directory in a std::filesystem::path?

I have a path to a directory and I want to get the name of that directory, using C++'s std::filesystem. For example, if the path was: std::filesystem::path fake_path("C:\\fake\\path\\to\\my_directory\\"); I would want to get "my_directory". I've…
Withad
  • 503
  • 1
  • 6
  • 12
13
votes
3 answers

Passing std::filesystem::path to a function segfaults

When I attempt to use std::filesystem::path as a function argument, it segfaults on my machine. Here is a minimal example: #include void thing(const std::filesystem::path& p) { return; } int main() { thing("test"); return…
Astrognome
  • 303
  • 3
  • 9
13
votes
1 answer

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"…
Klaus
  • 24,205
  • 7
  • 58
  • 113
12
votes
1 answer

What does std::filesystem::is_regular_file(path) mean on Windows?

About std::filesystem::is_regular_file(path), cppreference.com says: Checks if the given file status or path corresponds to a regular file […] Equivalent to s.type() == file_type::regular. For example, in the Linux kernel, file types are declared…
Amit
  • 645
  • 1
  • 3
  • 19
11
votes
4 answers

Why does std::filesystem provide so many non-member functions?

Consider for example file_size. To get the size of a file we will be using std::filesystem::path p = std::filesystem::current_path(); // ... usual "does this exist && is this a file" boilerplate auto n = std::filesystem::file_size(p); Nothing…
dlw
  • 323
  • 2
  • 14
10
votes
3 answers

No operator+ for std::filesystem::path?

It is possible to append multiple paths in a row using the / operator: std::filesystem::path p1{"A"}; auto p2 = p1 / "B" / "C"; which is rather convenient. However, concat only offers +=: std::filesystem::path p1{"A"}; auto p2 = p1 / "B" /…
Touloudou
  • 2,079
  • 1
  • 17
  • 28
10
votes
2 answers

Get the absolute path from std::filesystem::path c++

I have this piece of code auto path = std::filesystem::path("/root/home/../opt/."); I had tried std::filesystem::absolute() but then realized it is for something else than the reasult I want My question is how can i convert that relative path to…
Dev-il
  • 157
  • 1
  • 8
10
votes
2 answers

Can filesystem::canonical be used to prevent filepath injection for filepaths passed to fstream

I have a public folder pub with subfolders and files in it. A user gives me now a relative filepath, I perform some mappings, and I read the file with fstream and return it to the user. The problem is now if the user gives me a path like e.g.…
ezegoing
  • 526
  • 1
  • 4
  • 18
8
votes
1 answer

How to properly handle windows paths with the long path prefix with std::filesystem::path

std::filesystem::path doesn't seem to be aware of the windows long path magic prefix. Is this per design or is there a mode/flag/compiler switch/3rd party library which can be used? f.e. for a path like C:\temp\test.txt, the root_name is C: and the…
ridilculous
  • 624
  • 3
  • 16
1
2 3
10 11