Questions tagged [std-filesystem]
c++ std::filesystem discussion, std::filesystem was added to the ISO C++ standard library in C++17
147
questions
0
votes
1
answer
88
views
std::filesystem example not compiling on c++ 17
I cant compile this official cpp filesystem reference example using c++ 17 clang:
https://en.cppreference.com/w/cpp/filesystem/recursive_directory_iterator
#include <fstream>
#include <...
5
votes
3
answers
149
views
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 ...
0
votes
0
answers
43
views
Portable method in C++ to determine special folder locations (Downloads, Documents, Preferences, etc.)
I've been searching for a portable method in Boost or C++17 std::filesystem (or POSIX) to determine the system sanctioned locations of the user's Documents, Downloads, and Preferences. Do these ...
0
votes
0
answers
16
views
Problems when using std::filesystem with GCC8 on Centos7 [duplicate]
My developing/producing environments are all CentOS-7.9. And I have installed "devtoolset-8" on my developing env to use GCC8. But when I execute the code. I met some problems.
#include <...
-1
votes
1
answer
61
views
How can i get all files from disk?
I have to create a vector of all files on my disk, starting from root ("C:/").
I used filesystem library and recursive_directory_iterator, however program throws filesystem::filesystem_error ...
0
votes
0
answers
44
views
std::filesystem sees deleted files
Why std::filesystem sees deleted files?
for (auto& file : std::filesystem::recursive_directory_iterator(game_folder))
{
cout << file.path().string().c_str() << endl;
}
this code ...
12
votes
1
answer
804
views
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 ...
2
votes
3
answers
128
views
Check if a path is valid, even if it doesn't exist
Let's say I have a program that takes paths to the input and output directories from a command line. If the output directory doesn't exist, our program needs to create it. I would like to check (...
3
votes
0
answers
227
views
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 ...
0
votes
0
answers
100
views
Portably check whether a file is readable
I'm asking the cross-platform version of this question:
C++: How to check if a file/directory is readable? (PHP equivalent: is_readable)
I have a filesystem::path of a file (let's ignore for ...
0
votes
3
answers
264
views
Error with std::filesystem::copy copying a file to another pre-existing directory
See below for the following code, and below that, the error that follows.
std::string source = "C:\\Users\\cambarchian\\Documents\\tested";
std::string destination = "C:\\Users\\...
5
votes
1
answer
336
views
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 ...
0
votes
0
answers
163
views
C++ std::filesystem::last_write_time operator< sometimes not correct?
I want to compare the file age of two files. I tried this:
auto ft1 = std::filesystem::last_write_time("file1");
auto ft2 = std::filesystem::last_write_time("file2");
if (ft1 < ...
0
votes
1
answer
133
views
Can std::filesystem::permissions change secure file permissions
I have been experimenting, with the std::filesystem and I came across the permissions function, which allows you to change the access permissions that users have to files. This seems almost like it ...
0
votes
0
answers
23
views
Getting the age of a file in C++17 as a duration? (chrono/filesystem) [duplicate]
Please consider the following C++17 program:
#include <filesystem>
#include <chrono>
#include <iostream>
using namespace std::filesystem;
using namespace std::chrono;
int main(int ...
0
votes
1
answer
437
views
std::filesystem::remove_all fails on read-only file
I am trying to remove a complete git repository clone from my disk using std::filesystem::remove_all()
std::filesystem::remove_all( myRepoName );
This throws an exception
terminate called after ...
0
votes
0
answers
15
views
std::time_t to std::filesystem::file_time_t in C++ 17 [duplicate]
I understand that in C++ 20, you can cast a std::time_t value to std::filesystem::file_time_t with
std::chrono::clock_cast<std::chrono::file_clock>(std::chrono::system_clock::from_time_t((time_t)...
0
votes
1
answer
134
views
C++ MFC reading Cstring from editbox for file reading (std::filesystem Exception unhandled memory problem)
What my code is suppose to achieve is to read in the file names after given a file path input and output (switch 1: the files under the same folder)(switch 2: all the file names under the directory ...
1
vote
1
answer
116
views
recursive_directory_iterator's skip_permission_denied option appears to be ignored on macOS?
Using C++20 and std::filesystem::recursive_directory_iterator on macOS, this code:
for (auto& f : recursive_directory_iterator(getenv("HOME"), directory_options::skip_permission_denied)) ...
0
votes
0
answers
258
views
Unable to get path from std::filesystem in global static object
Build Environment:
Visual Studio 2022
Windows 10 64-bit
C++ Standard : C++20
I am trying to get the directory of my executable file. In my earlier code based on C++17 standard I could read the path ...
1
vote
1
answer
67
views
What type would you use for passing file paths in your C++ library API?
Since I started using C++17 every time I have to develop a new library I consider several alternatives for passing file paths to public functions/methods. These are some of my thoughts I normally have ...
4
votes
0
answers
159
views
create all folder in a big path (with subfolders) [closed]
Im trying to create all folder in a path that reach to a file, for that by a little search I found out about std::filesystem::create_directories its ok but the problem of using it is looks like its ...
1
vote
1
answer
194
views
‘operator/’ is not a member of ‘std::filesystem’; did you mean ‘operator~'
I am trying to install the MMMTools https://mmm.humanoids.kit.edu/installation.html. My cmake version is 3.16.3. I went through every step without any errors until this section
cd ~/MMMCore
mkdir ...
0
votes
1
answer
95
views
Cannot use filsesystem in ROS node
I'm trying to use std::filesystem to open a folder and iterate through its files, sorted by name.
I'm using the code from this question inside a very basic ros package:
node.cpp
#include <ros/ros.h&...
0
votes
0
answers
101
views
Is there an equivelent of Win32 API MoveFileEx() in C++ filesystem?
I am porting a codebase that depends on Win32 API to C++ 17. There is a class that is responsible for handling files. The function I am currently looking at looks like this:
bool project::filesystem::...
0
votes
0
answers
320
views
Segmentation fault with std::filesystem path object
I have recently started using std::filesystem, and I have faced an issue that I could not fix. I have a class with a function that periodically iterates over some directories and in case new files are ...
0
votes
1
answer
181
views
How to check if std::filesystem::copy is ended?
Copying files to a directory to another directory using this code
auto const copyOption = std::filesystem::copy_options::recursive | std::filesystem::copy_options::skip_symlinks;
std::filesystem::copy(...
0
votes
0
answers
44
views
Difficulty implementing C++17 <filesystem> [duplicate]
I have been trouble getting std::filesystem to compile using gcc and clang. Below is my current version of g++ and clang respectively:
g++ --version
g++ (Debian 10.2.1-6) 10.2.1 20210110
clang --...
1
vote
1
answer
197
views
Q: Boost Program Options using std::filesystem::path as option fails when the given path contains spaces
I have a windows command line program using Boost.Program_Options. One option uses a std::filesystem::path variable.
namespace fs = std::filesystem;
namespace po = boost::program_options;
fs::path ...
6
votes
1
answer
154
views
`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 ...
4
votes
2
answers
205
views
Why can't I insert this transformed directory_iterator into a vector?
I am trying to insert a transformed range of directory entries into a vector using its insert(const_iterator pos, InputIt first, InputIt last) member function template.
Unfortunately I can't get the ...
0
votes
2
answers
436
views
iterating through all the directories and subdirectories in c++
I wanted to use the std::filesystem::recursive_directory_iterator class to create a class method iterating through all subdirectories and processing found xml files.
The only way I have found on the ...
1
vote
1
answer
163
views
No reverse iterators for `std::filesystem::path`?
Is there a technical reason why std::filesystem::path doesn't offer reverse iterators (i.e., rbegin and rend)?
If I have a std::filesystem::path for /a/b/c/b/d/b/e and I want to find the first ...
0
votes
1
answer
159
views
How to reset filesystem::current_path() in C++17?
I am writing a C++ program in which I change the working directory with std::filesystem::current_path(working_directory), where working_directory is a string. Is there a good way to, later in the ...
3
votes
1
answer
134
views
How to skip/disregard files with names that use wide characters using std::filesystem?
I am iterating through a large set of nested directories searching for files of some extension, say ".foo" using code like the following:
namespace fs = std::filesystem;
int main(int argc, ...
3
votes
2
answers
389
views
I don't know how to use filesystem to look for .txt files c++
I would like to use std::filesystem in my project, which will allow me to show .txt files in the current directory (I use Ubuntu, I don't need a Windows function because I have already seen one on ...
2
votes
2
answers
402
views
Potential bug in std::filesystem::remove_all with clang++
DO NOT TRY THIS AT HOME
I am having a weird issue with std::filesystem::remove_all.
I have written a program that writes N files to disk in a single directory and then deletes all the files afterward (...
3
votes
1
answer
403
views
UTF8 to UTF16 conversion using std::filesystem::path
Starting from C++11 one can convert UTF8 to UTF16 wchar_t (at least on Windows, where wchar_t is 16 bit wide) using std::codecvt_utf8_utf16:
std::wstring utf8ToWide( const char* utf8 )
{
std::...
0
votes
1
answer
222
views
C++ directory item iteration without exceptions
In C++17 it became easy to iterate over the items in some directory dir:
for ( auto& dirEntry: std::filesystem::directory_iterator(dir) )
{
if ( !dirEntry.is_regular_file() ) continue;
...
...
1
vote
2
answers
227
views
VS error from std::filesystem::u8path(const char8_t*)
In this simple C++20 program
#define _SILENCE_CXX20_U8PATH_DEPRECATION_WARNING //suppress VS warning
#include <filesystem>
int main()
{
auto p = std::filesystem::u8path(u8"a");
}
...
2
votes
1
answer
63
views
Non-empty relative current path, in standard C++?
With C++17 (or Boost::filesystem), we can get the current path / current working directory using filesystem::current_path(). However - that gives us an absolute path.
We could also use an empty path ...
0
votes
1
answer
214
views
Why abort() method is called sometimes when I iterate over directory using std::filesystem::directory_terator?
I am creating a simple file watcher in C++. I am using std::filesystem::directory_iterator and sometimes it crashes with abort() being called. It works perfectly when I track creating and editing of ...
5
votes
1
answer
1k
views
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 ...
2
votes
0
answers
121
views
Is it possible to refactor _wfopen to std::filesystem?
I want to refactor this code in C++17. Can I use std::filesystem in this case?
#if defined(BOOSTER_WIN_NATIVE)
bool open(std::string const &file_name,std::string const &encoding)
{
...
0
votes
0
answers
325
views
Check if user has the permission to create a file at path p, without creating a file at the path
In C++ is there a way to check, if we have the permission to create a file at a certain path without actually creating a file?
I already read How do I create a file with boost filesystem without ...
0
votes
0
answers
257
views
Visual Studio 2019 C++ and std::filesystem changing to ISO C++17 Standard or adding _HAS_CXX17 1 do not work
based on this thread it is still not working although i changed to ISO C++17 Standard (/std:c++17)
in c++ -> language in VS 2019 config.
also try to set :
#ifdef _WIN32
#define _HAS_CXX17 1
#...
8
votes
3
answers
638
views
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, ...
6
votes
2
answers
455
views
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?
#...
0
votes
4
answers
258
views
std::filesystem:file_size send me an incoherent value
In order to read a file in my code, I use std::filesystem::file_size (https://en.cppreference.com/w/cpp/filesystem/file_size) to get his size. I use this code :
template <typename TYPE>
inline ...
1
vote
1
answer
85
views
Getting the path of recursively iterated directories relative to the specified entry path
Consider the example in the documentation of std::filesystem::recursive_directory_iterator:
#include <fstream>
#include <iostream>
#include <filesystem>
namespace fs = std::...