• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    function
    <fstream>

    std::basic_ofstream::コンストラクタ

    basic_ofstream(); // (1)
    explicit basic_ofstream(const char* s, ios_base::openmode mode = ios_base::out); // (2)
    explicit basic_ofstream(const string& s, ios_base::openmode mode = ios_base::out); // (3)
    explicit basic_ofstream(const filesystem::path::value_type* s,
                           ios_base::openmode mode = ios_base::out); // (4) C++17
    explicit basic_ofstream(const filesystem::path& s,
                           ios_base::openmode mode = ios_base::out); // (5) C++17
    basic_ofstream(const basic_ofstream& rhs) = delete; // (6) C++11
    basic_ofstream(basic_ofstream&& rhs); // (7) C++11
    

    概要

    オブジェクトを構築する。一部のオーバーロードでは、ファイルを開く機能を持っている。

    効果

    • (1) : デフォルトコンストラクタ。空の状態にする。
    • (2) : 仮引数sで指定したファイルを開く。
    • (3) : ファイルを指定する引数の型がstd::stringである点を除き、(2)と同じ。
    • (4) : std::filesystem::path::value_typeの型がcharではないときのみ定義される。効果は(2)と同じ。
    • (5) : ファイルを指定する引数の型がstd::filesystem::pathである点を除き、(2)と同じ。
    • (6) : コピーコンストラクタ。コピー不可。
    • (7) : ムーブコンストラクタ。ファイルストリームの所有権を移動する。

    #include <iostream>
    #include <fstream>
    
    int main()
    {
      std::ofstream s1("file.txt");
      if (!s1) {
        std::cerr << "file.txtを開けませんでした。" << std::endl;
      }
    
      try
      {
        std::ofstream s2("internal.dat", std::ios_base::in | std::ios_base::out | std::ios_base::binary);
        s2.exceptions(std::ios_base::failbit);
      } catch (const std::exception& e) {
        std::cerr << "internal.datを開けませんでした。" << std::endl;
      }
    }
    

    出力

    internal.datを開けませんでした。
    

    実装例

    例示のため、basic_ofstream<>が内部で保持しているbasic_filebufオブジェクトを、仮にメンバ変数sbとする。

    // (1)
    template<class CharT, class Traits>
    basic_ofstream<CharT, Traits>::basic_ofstream()
    //: basic_istream(&sb), sb() {           // C++98
      : basic_istream(addressof(sb)), sb() { // C++11
      // 本体は空
    }
    
    // (2)
    template<class CharT, class Traits>
    basic_ofstream<CharT, Traits>::basic_ofstream(const char* s, ios_base::openmode mode)
    //: basic_istream(&sb), sb() {           // C++98
      : basic_istream(addressof(sb)), sb() { // C++11
      if (rdbuf()->open(s, mode | ios_base::out) == nullptr) {
        setstate(failbit);
      }
    }
    
    // (3)
    template<class CharT, class Traits>
    basic_ofstream<CharT, Traits>::basic_ofstream(const string& s, ios_base::openmode mode)
      : basic_ofstream(s.c_str(), mode) {
      // 本体は空
    }
    
    // (5)
    template<class CharT, class Traits>
    basic_ofstream<CharT, Traits>::basic_ofstream(basic_ofstream&& rhs)
      : basic_istream(move(rhs)), sb(move(rhs.sb)) {
      // set_rdbuf(&sb);        // C++98
      set_rdbuf(addressof(sb)); // C++11
    }
    

    バージョン

    言語

    • C++98
    • C++11: ムーブコンストラクタの追加
    • C++17: std::filesystem::pathへの対応

    参照