boost-filesystem

Linker errors when using boost::filesystem?

谁都会走 提交于 2019-12-06 21:32:04
问题 I have the following code: #include <iostream> #include <boost\filesystem.hpp> int main(){ const char* file_path = "my_path"; std::cout << boost::filesystem::file_size(file_path) << std::endl; } and when I build I get the following errors: 1>Main.obj : error LNK2019: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::system_category(void)" (?system_category@system@boost@@YAAEBVerror_category@12@XZ) referenced in function "void __cdecl boost::system:

Read/Write file with unicode file name with plain C++/Boost

微笑、不失礼 提交于 2019-12-05 20:03:14
问题 I want to read / write a file with a unicode file name using boost filesystem, boost locale on Windows (mingw) (should be platform independent at the end). This is my code: #include <boost/locale.hpp> #define BOOST_NO_CXX11_SCOPED_ENUMS #include <boost/filesystem.hpp> #include <boost/filesystem/fstream.hpp> namespace fs = boost::filesystem; #include <string> #include <iostream> int main() { std::locale::global(boost::locale::generator().generate("")); fs::path::imbue(std::locale()); fs::path

parent_path() with or without trailing slash

女生的网名这么多〃 提交于 2019-12-05 18:48:42
As explained in the documentation , the expected output of the following is: boost::filesystem::path filePath1 = "/home/user/"; cout << filePath1.parent_path() << endl; // outputs "/home/user" boost::filesystem::path filePath2 = "/home/user"; cout << filePath2.parent_path() << endl; // outputs "/home" The question is, how do you deal with this? That is, if I accept a path as an argument, I don't want the user to care whether or not it should have a trailing slash. It seems like the easiest thing to do would be to append a trailing slash, then call parent_path() TWICE to get the parent path of

boost::filesystem::path and fopen()

陌路散爱 提交于 2019-12-05 16:05:32
问题 I get error when I try to do this: path p = "somepath"; FILE* file = fopen(p.c_str(), "r"); I get: argument of type "const boost::filesystem::path::value_type *" is incompatible with parameter of type "const char *" Could anyone tell me what I'm doing wrong? Thanks 回答1: If you're under Windows, that value_type is wchar_t , and will fail in the conversion for fopen (that needs a char* ). As per the documentation, it seems you have to use the string() method to obtain a standard string with a

Traversing a directory with boost::filesystem without throwing exceptions

青春壹個敷衍的年華 提交于 2019-12-05 08:26:01
I have a path to the directory, and I want to traverse through all of its sub-directories, collecting files' pathes by the way. namespace fs = boost::filesystem; std::vector<fs::path> traverse_if_directory(fs::path& f) { std::vector<fs::path> result; if (fs::is_directory(f)) { for (fs::recursive_directory_iterator it(f), eit; it != eit; ++it) { if (!fs::is_directory(it->path())) { result.push_back(it->path()); } } } else { result.push_back(f); } return result; } Unfortunately, in the middle of the traversing, I stumble upon a directory which I have no rights to look at, and the code above

how to get file names vs directory names in c++ (using boost filesystem library)

偶尔善良 提交于 2019-12-05 06:47:44
问题 When I use boost::filesystem to get a list of file names in a directory, I receive file names as well as directory names: #include <string> #include <iostream> #include <boost/filesystem.hpp> using namespace std; using namespace boost::filesystem; int main() { path p("D:/AnyFolder"); for (auto i = directory_iterator(p); i != directory_iterator(); i++) { cout << i->path().filename().string() << endl; } } Output is like: file1.txt file2.dat Folder1 //which is a folder Is there a quick way to

How do I ignore hidden files (and files in hidden directories) with Boost Filesystem?

﹥>﹥吖頭↗ 提交于 2019-12-05 03:31:10
I am iterating through all files in a directory recursively using the following: try { for ( bf::recursive_directory_iterator end, dir("./"); dir != end; ++dir ) { const bf::path &p = dir->path(); if(bf::is_regular_file(p)) { std::cout << "File found: " << p.string() << std::endl; } } } catch (const bf::filesystem_error& ex) { std::cerr << ex.what() << '\n'; } But this includes hidden files and files in hidden directories. How do I filter out these files? If needed I can limit myself to platforms where hidden files and directories begin with the '.' character. Unfortunately there doesn't seem

C++: All boost path operations segfault (OSX / GCC)

末鹿安然 提交于 2019-12-04 04:37:12
I am getting consistent segfaults with almost any operation I am trying to perform with boost path. ( Edit: It appears that all segfaulting functions are related to current_path() ) Sample program: #include <boost/filesystem/operations.hpp> #include <boost/filesystem/path.hpp> #include <iostream> using namespace std; using namespace boost::filesystem; using namespace boost::system; int main(int argc, const char * argv[]) { error_code err; auto p = path("hello/../world"); cout << p.string() << endl; path c = canonical(p, err); cout << c.string() << endl; } The above is just an example, the

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

主宰稳场 提交于 2019-12-04 03:28:25
问题 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

Why do boost::filesystem::path and std::filesystem::path lack operator+?

家住魔仙堡 提交于 2019-12-04 02:50:53
问题 Consider the following assertions about path decomposition, where each local variable e.g. stem has the obvious initialization e.g. auto stem = path.stem() — assert(root_path == root_name / root_directory); assert(path == root_name / root_directory / relative_path); assert(path == root_path / relative_path); assert(path == parent_path / filename); assert(filename == stem + extension); This all works, except for the last line — because fs::path does not define an operator+ . It has operator+=