Obtain platform's path separator using Boost.Filesystem

前端 未结 3 1923
梦毁少年i
梦毁少年i 2021-02-05 10:16

Is there a way to obtain the platform\'s path separator character using Boost.Filesystem? By path separator, I mean / for Unix and \\ for Windows.

相关标签:
3条回答
  • 2021-02-05 11:04

    It seems like boost::filesystem::path::make_preferred is the ticket:

    Effects: The contained pathname is converted to the preferred native format. [Note: On Windows, the effect is to replace slashes with backslashes. On POSIX, there is no effect. -- end note]

    Example:

    namespace bfs = boost::filesystem;
    bfs::path slash("/");
    bfs::path::string_type preferredSlash = slash.make_preferred().native();
    
    0 讨论(0)
  • 2021-02-05 11:12

    As of version 1.57, Boost now has a better solution, that is just constant char / wchar_t ( dependent on different platforms ): boost::filesystem::path::preferred_separator.

    Read http://www.boost.org/doc/libs/release/libs/filesystem/doc/reference.html#Operating-system-examples for more information. There are even more system-dependent features in it.

    Simple example:

    #include <boost/filesystem.hpp>
    #include <iostream>
    
    int main() {
        std::cout << boost::filesystem::path::preferred_separator << std::endl;
    }
    
    0 讨论(0)
  • 2021-02-05 11:12

    Haven't tested this, but it looks like you should be able to use this on a recent boost:

    http://www.boost.org/doc/libs/1_43_0/libs/filesystem/doc/reference.html

    #include <boost/filesystem.hpp>
    #include <iostream>
    
    int main() {
        std::cout << boost::filesystem::slash<boost::filesystem::path>::value << std::endl;
    }
    
    0 讨论(0)
提交回复
热议问题