Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_fstream<CharT,Traits>::basic_fstream

      From cppreference.com
      <cpp‎ |io‎ |basic fstream

      [edit template]
       
       
       
       
      basic_fstream();
      (1)
      explicit basic_fstream(constchar* filename,

                             std::ios_base::openmode mode

                                 =std::ios_base::in|std::ios_base::out);
      (2)
      explicit basic_fstream(const std::filesystem::path::value_type* filename,

                             std::ios_base::openmode mode

                                 =std::ios_base::in|std::ios_base::out);
      (3)(since C++17)
      explicit basic_fstream(conststd::string& filename,

                             std::ios_base::openmode mode

                                 =std::ios_base::in|std::ios_base::out);
      (4)(since C++11)
      template<class FsPath>

      explicit basic_fstream(const FsPath& filename,
                             std::ios_base::openmode mode

                                 =std::ios_base::in|std::ios_base::out);
      (5)(since C++17)
      basic_fstream( basic_fstream&& other);
      (6)(since C++11)
      basic_fstream(const basic_fstream& rhs)= delete;
      (7)(since C++11)

      Constructs new file stream.

      1) Default constructor: constructs a stream that is not associated with a file: default-constructs thestd::basic_filebuf and constructs the base with the pointer to this default-constructedstd::basic_filebuf member.
      2,3) First, performs the same steps as the default constructor, then associates the stream with a file by callingrdbuf()->open(filename, mode) (seestd::basic_filebuf::open for the details on the effects of that call). If theopen() call returns a null pointer, setssetstate(failbit).Overload(3) is only provided ifstd::filesystem::path::value_type is notchar.(since C++17)
      4,5) Same asbasic_fstream(filename.c_str(), mode).(5) participates in overload resolution only ifFsPath isstd::filesystem::path.(since C++17)
      6) Move constructor. First, move-constructs the base class fromother (which does not affect therdbuf() pointer), then move-constructs thestd::basic_filebuf member, then callsthis->set_rdbuf() to install the newbasic_filebuf as therdbuf() pointer in the base class.
      7) The copy-constructor is deleted: this class is not copyable.

      Contents

      [edit]Parameters

      filename - the name of the file to be opened
      mode - specifies stream open mode. Following constants and bit-wise OR between them may be used:
      Constant Explanation
      app seek to the end of stream before each write
      binary open inbinary mode
      in open for reading
      out open for writing
      trunc discard the contents of the stream when opening
      ate seek to the end of stream immediately after open
      noreplace(C++23) open in exclusive mode
      other - another file stream to use as source

      [edit]Example

      Run this code
      #include <fstream>#include <string>#include <utility> int main(){std::fstream f0;std::fstream f1("test.bin", std::ios::binary);std::string name="example.txt";std::fstream f2(name);std::fstream f3(std::move(f1));}

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 460C++98the default argument ofmode in overload(2)
      was missing (it is present in thesynopsis)
      added
      LWG 3430C++17std::filesystem::path overload led to unwanted conversionsavoided by making it a template

      [edit]See also

      opens a file and associates it with the stream
      (public member function)[edit]
      opens a file and configures it as the associated character sequence
      (public member function ofstd::basic_filebuf<CharT,Traits>)[edit]
      replaces therdbuf without clearing its error state
      (protected member function)[edit]
      constructs the object
      (public member function ofstd::basic_iostream<CharT,Traits>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_fstream/basic_fstream&oldid=158147"

      [8]ページ先頭

      ©2009-2025 Movatter.jp