std::filesystem::path::string, std::filesystem::path::wstring, std::filesystem::path::u8string, std::filesystem::path::u16string, std::filesystem::path::u32string
cppreference.net에서
template< class CharT, class Traits = std::char_traits<CharT>,
class Alloc = std::allocator<CharT> >
std::basic_string<CharT,Traits,Alloc>
string( const Alloc& a = Alloc() ) const;
|
(1) | (C++17부터) |
std::string string() const;
|
(2) | (C++17부터) |
std::wstring wstring() const;
|
(3) | (C++17부터) |
std::u16string u16string() const;
|
(4) | (C++17부터) |
std::u32string u32string() const;
|
(5) | (C++17부터) |
std::string u8string() const;
|
(6) | (C++17부터) (C++20까지) |
std::u8string u8string() const;
|
(C++20부터) | |
네이티브 경로 이름 형식의 내부 경로 이름을 특정 문자열 형식으로 변환하여 반환합니다. 변환(있는 경우)은 다음과 같이 수행됩니다.
- 만약
path::value_type가char이면, 변환(있는 경우)은 시스템에 따라 달라집니다. 이는 일반적인 POSIX 시스템(예: Linux)에서 해당하는 경우로, 네이티브 인코딩은 UTF-8이고string()은 변환을 수행하지 않습니다. - 그렇지 않고,
path::value_type가wchar_t이면, 변환(있는 경우)은 지정되지 않습니다. 이는 Windows에서 해당하는 경우로,wchar_t은 16비트이고 네이티브 인코딩은 UTF-16입니다. - 그렇지 않고,
path::value_type가char16_t이면, 네이티브 인코딩은 UTF-16이고 변환 방법은 지정되지 않습니다. - 그렇지 않고,
path::value_type가char32_t이면, 네이티브 인코딩은 UTF-32이고 변환 방법은 지정되지 않습니다. - 그렇지 않고,
path::value_type가char8_t이면, 네이티브 인코딩은 UTF-8이고 변환 방법은 지정되지 않습니다.
1) 모든 메모리 할당은
a 에 의해 수행됩니다.6)
u8string() 의 경우 결과 인코딩은 항상 UTF-8입니다.반환값
네이티브 경로명 형식의 내부 경로명을 지정된 문자열 타입으로 변환합니다.
예외
구현 정의 예외를 던질 수 있습니다.
예제
이 코드 실행
#include <clocale> #include <cstdio> #include <filesystem> #include <fstream> #include <iostream> #include <locale> int main() { const char* const localeName = "ja_JP.utf-8"; std::setlocale(LC_ALL, localeName); std::locale::global(std::locale(localeName)); const std::filesystem::path p(u8"要らない.txt"); std::ofstream(p) << "File contents"; // 네이티브 문자열 표현은 OS API와 함께 사용 가능 if (std::FILE* const f = std::fopen(p.string().c_str(), "r")) { for (int ch; (ch = std::fgetc(f)) != EOF;) std::putchar(ch); std::fclose(f); } // 멀티바이트 및 와이드 표현은 출력에 사용 가능 std::cout << "\nFile name in narrow multibyte encoding: " << p.string() << '\n'; // wstring()은 stdlibc++에서 예외를 발생시킴 (gcc-12.1.0 기준), 참조: // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95048 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102839 // 최신 gcc-12.2.1 (2023/02/01) 및 clang-10+에서 작동 std::wcout << "File name in wide encoding: " << p.wstring() << '\n'; std::filesystem::remove(p); }
가능한 출력:
File contents File name in narrow multibyte encoding: 要らない.txt File name in wide encoding: 要らない.txt
참고 항목
|
일반 경로명 형식으로 변환된 경로를 문자열로 반환합니다
(public member function) |