Namensräume
Varianten
Aktionen

std::filesystem::path::c_str, std::filesystem::path::native, std::filesystem::path::operator string_type()

Von cppreference.com
< cpp‎ | filesystem‎ | path
 
 
 
 
const value_type* c_str() const noexcept;
(1) (seit C++17)
const string_type& native() const noexcept;
(2) (seit C++17)
operator string_type() const;
(3) (seit C++17)

Greift auf den nativen Pfadnamen als Zeichenkette zu.

1) Äquivalent zu native().c_str().
2) Gibt die Repräsentation des Pfadnamens im nativen Format als Referenz zurück.
3) Gibt die Repräsentation des Pfadnamens im nativen Format als Wert zurück.

Inhalt

[bearbeiten] Parameter

(keine)

[bearbeiten] Rückgabewert

Die native String-Repräsentation des Pfadnamens unter Verwendung nativer Syntax, nativem Zeichentyp und nativer Zeichenkodierung. Dieser String ist für die Verwendung mit Betriebssystem-APIs geeignet.

[bearbeiten] Hinweise

Die Konvertierungsfunktion (3) ist vorgesehen, damit APIs, die std::basic_string-Dateinamen akzeptieren, Pfadnamen ohne Codeänderungen verwenden können.

[bearbeiten] Beispiel

#include <cstdio>
#ifdef _MSC_VER
#include <fcntl.h>
#include <io.h>
#else
#include <clocale>
#include <locale>
#endif
#include <filesystem>
#include <fstream>
 
int main()
{
#ifdef _MSC_VER
    _setmode(_fileno(stderr), _O_WTEXT);
#else
    std::setlocale(LC_ALL, "");
    std::locale::global(std::locale(""));
#endif
 
    std::filesystem::path p(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-specific APIs
#ifdef _MSC_VER
    if (std::FILE* f = _wfopen(p.c_str(), L"r"))
#else
    if (std::FILE* f = std::fopen(p.c_str(), "r"))
#endif
    {
        for (int ch; (ch = fgetc(f)) != EOF; std::putchar(ch))
        {}
        std::fclose(f);
    }
 
    std::filesystem::remove(p);
}

Mögliche Ausgabe

File contents

[bearbeiten] Siehe auch

gibt den Pfad im nativen Pfadnamen-Format als String zurück
(public member function) [edit]
gibt den Pfad im generischen Pfadnamen-Format als String zurück
(public member function) [edit]