std::filesystem::path::c_str
STD::文件系统::path::C[医]Str,std::file system::path::本机,std::file system::path::Operator String[医]类型%28%29
const value_type* c_str() const; | (1) | (since C++17) |
---|---|---|
const string_type& native() const; | (2) | (since C++17) |
operator string_type() const; | (3) | (since C++17) |
以字符串的形式访问本机路径名。
1%29相当于native().c_str()
...
2%29通过引用返回路径名的本机字符串表示形式。
3%29按值返回路径名的本机字符串表示形式。
参数
%280%29
返回值
路径名的本机字符串表示形式,使用本机语法、本机字符类型和本机字符编码。此字符串适合用于OSAPI。
例外
1,2%29
noexcept
规格:
noexcept
注记
提供了转换函数%283%29,以便标准的文件打开api接受std::basic_string
文件名,例如std::ifstream
构造函数,可以使用路径名而不更改代码:
二次
std::filesystem::path p = "/tmp/text.txt";
std::ifstream f(p
二次
例
二次
#include <cstdio>
#ifdef _MSC_VER
#include <io.h>
#include <fcntl.h>
#else
#include <locale>
#include <clocale>
#endif
#include <fstream>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
#ifdef _MSC_VER
_setmode(_fileno(stderr), _O_WTEXT
#else
std::setlocale(LC_ALL, ""
std::locale::global(std::locale("")
std::cout.imbue(std::locale()
std::wcerr.imbue(std::locale()
#endif
fs::path p = fs::u8path(u8"要らない.txt"
std::ofstream(p) << "File contents"; // Prior to LWG2676 uses operator string_type()
// on MSVC, where string_type is wstring, only
// works due to non-standard extension.
// Post-LWG2676 uses new fstream constructors
// native string representation can be used with OS APIs
if (std::FILE* f =
#ifdef _MSC_VER
_wfopen(p.c_str(), L"r")
#else
std::fopen(p.c_str(), "r")
#endif
)
{
int ch;
while((ch=fgetc(f))!= EOF) putchar(ch
std::fclose(f
}
// multibyte and wide representation can be used for output
std::cout << "\nFile name in narrow multibyte encoding: " << p.string() << '\n';
std::wcerr << "File name in wide encoding: " << p.wstring() << '\n';
fs::remove(p
}
二次
产出:
二次
File contents
File name in narrow multibyte encoding: 要らない.txt
File name in wide encoding: 要らない.txt
二次
另见
stringwstringu8stringu16stringu32string | returns the path in native pathname format converted to a string (public member function) |
---|---|
generic_stringgeneric_wstringgeneric_u8stringgeneric_u16stringgeneric_u32string | returns the path in generic pathname format converted to a string (public member function) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。