boost-filesystem

how to make a copy of boost::filesystem::directory_iterator?

最后都变了- 提交于 2019-12-01 18:03:58
I know this sounds stupid, but look at this simple example (working dir should have more than one item): #define BOOST_FILESYSTEM_VERSION 3 #include <boost/filesystem.hpp> #include <cassert> int main() { using namespace boost::filesystem; directory_iterator it("./"); directory_iterator it_copy = it; ++it; assert(it_copy != it); return 0; } it_copy is modified together with it ! (boost 1.45) What considerations could lead to such design ( directory_iterator is something like smart ptr)? I just need to save a copy of directory_iterator to use it later. If you take a look at the reference you

Boost::file_system: Checking error codes

坚强是说给别人听的谎言 提交于 2019-12-01 03:39:13
问题 Although I'm using C++11, this question is boost-related, since I'm processing errors from boost::file_system . In the following situation: try { // If p2 doesn't exists, canonical throws an exception // of No_such_file_or_directory path p = canonical(p2); // Other code } catch (filesystem_error& e) { if (e is the no_such_file_or_directory exception) custom_message(e); } // other catchs } If I print the error value when the desired exception (no_such_file_or_directory) is thrown: // ... }

How to create a folder in the home directory?

扶醉桌前 提交于 2019-11-30 20:06:23
I want to create a directory path = "$HOME/somedir" . I've tried using boost::filesystem::create_directory(path) , but it fails - apparently the function doesn't expand system variables. How can I do it the simplest way? (note: in my case the string path is constant and I don't know for sure if it contains a variable) edit: I'm working on Linux (although I'm planning to port my app to Windows in the near future). Fred Nurk Use getenv to get environment variables, including HOME . If you don't know for sure if they might be present, you'll have to parse the string looking for them. You could

boost::filesystem get relative path

天大地大妈咪最大 提交于 2019-11-30 07:59:05
What methods of the boost::filesystem library can help me to get a path relative to another path? I have a path /home/user1/Downloads/Books and a path /home/user1/ . Now I want to get a path Downloads/Books . Taken from a link found by following the ticket Nicol linked to: template < > path& path::append< typename path::iterator >( typename path::iterator begin, typename path::iterator end, const codecvt_type& cvt) { for( ; begin != end ; ++begin ) *this /= *begin; return *this; } // Return path when appended to a_From will resolve to same as a_To boost::filesystem::path make_relative( boost:

how to perform boost::filesystem copy_file with overwrite

一笑奈何 提交于 2019-11-29 10:34:41
问题 The Windows API function CopyFile has an argument BOOL bFailIfExists that allows you to control whether or not you want to overwrite the target file if it exists. The boost::filesystem copy_file function has no such argument, and will fail if the target file exists. Is there an elegant way to use the boost copy_file function and overwrite the target file? Or is it better to simply use the Windows API? My current target platform is Windows, but I prefer to use STL and boost where possible to

boost::filesystem get relative path

妖精的绣舞 提交于 2019-11-29 05:11:40
问题 What methods of the boost::filesystem library can help me to get a path relative to another path? I have a path /home/user1/Downloads/Books and a path /home/user1/ . Now I want to get a path Downloads/Books . 回答1: Taken from a link found by following the ticket Nicol linked to: template < > path& path::append< typename path::iterator >( typename path::iterator begin, typename path::iterator end, const codecvt_type& cvt) { for( ; begin != end ; ++begin ) *this /= *begin; return *this; } //

boost directory_iterator example - how to list directory files not recursive

走远了吗. 提交于 2019-11-28 21:07:52
How should I use directory_iterator to list directory files (not recursive)? Also what header files / libs should I add/link or other settings I should make? I'm using boost in my project but by some reason directory_iterator is "underclared identifier" while I can use other boost features. Update Another solution: #include <filesystem> #include <boost/filesystem.hpp> #include <iostream> using namespace boost::filesystem; for (directory_iterator itr(path_ss); itr!=directory_iterator(); ++itr) { cout << itr->path().filename() << ' '; // display filename only if (is_regular_file(itr->status()))

expand file names that have environment variables in their path

僤鯓⒐⒋嵵緔 提交于 2019-11-27 12:25:41
What's the best way to expand ${MyPath}/filename.txt to /home/user/filename.txt or %MyPath%/filename.txt to c:\Documents and settings\user\filename.txt with out traversing the path string looking for environement variables directly? I see that wxWidgets has a wxExpandEnvVars function. I can't use wxWidgets in this case, so I was hoping to find a boost::filesystem equivalent or similar. I am only using the home directory as an example, I am looking for general purpose path expansion. For UNIX (or at least POSIX) systems, have a look at wordexp : #include <iostream> #include <wordexp.h> using

How similar are Boost filesystem and the standard C++ filesystem libraries?

吃可爱长大的小学妹 提交于 2019-11-26 21:08:42
问题 I need a filesystem library for use with a C++11-capable compiler or a C++14-capable one - so it can't be be from C++17. Now, I know that the filesystem library going into C++17 is based based on Boost::Filesystem; but - are they similar enough for me to use the Boost library and then seamlessly switch to the standard version at a later time, without changing more than, say, a using statement? Or are there (minor/significant) differences between the two? I know that for the case of variant ,