how to convert filesystem path to string

To convert a std::filesystem::path to a natively-encoded string (whose type is std::filesystem::path::value_type), use the string() method. Note the other *string() methods, which enable you to obtain strings of a specific encoding (e.g. u8string() for an UTF-8 string).

C++17 example:

#include <filesystem>
#include <string>

namespace fs = std::filesystem;

int main()
{
    fs::path path{fs::u8path(u8"愛.txt")};
    std::string path_string{path.u8string()};
}

C++20 example (better language and library UTF-8 support):

#include <filesystem>
#include <string>

namespace fs = std::filesystem;

int main()
{
    fs::path path{u8"愛.txt"};
    std::u8string path_string{path.u8string()};
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)