Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

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

      [edit template]
       
       
       
       
      void open(constchar* filename,

                 std::ios_base::openmode mode

                     =std::ios_base::in|std::ios_base::out);
      (1)
      void open(const std::filesystem::path::value_type* filename,

                 std::ios_base::openmode mode

                     =std::ios_base::in|std::ios_base::out);
      (2)(since C++17)
      void open(conststd::string& filename,

                 std::ios_base::openmode mode

                     =std::ios_base::in|std::ios_base::out);
      (3)(since C++11)
      void open(conststd::filesystem::path& filename,

                 std::ios_base::openmode mode

                     =std::ios_base::in|std::ios_base::out);
      (4)(since C++17)

      Opens and associates the file with namefilename with the file stream.

      Callsclear() on success. Callssetstate(failbit) on failure.

      1,2) Effectively callsrdbuf()->open(filename, mode) (seestd::basic_filebuf::open for the details on the effects of that call).Overload(2) is only provided ifstd::filesystem::path::value_type is notchar.(since C++17)
      3,4) Effectively calls(1,2) as if byopen(filename.c_str(), mode).

      Contents

      [edit]Parameters

      filename - the name of the file to be opened
      mode - specifies stream open mode. It is aBitmaskType, the following constants are defined:
      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

      [edit]Example

      Run this code
      #include <fstream>#include <iostream>#include <string> int main(){std::string filename="example.123"; std::fstream fs;    fs.open(filename); if(!fs.is_open()){        fs.clear();        fs.open(filename, std::ios::out);// create file        fs.close();        fs.open(filename);} std::cout<<std::boolalpha;std::cout<<"fs.is_open() = "<< fs.is_open()<<'\n';std::cout<<"fs.good() = "<< fs.good()<<'\n';}

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 22C++98it was unclear how error state changes upon a successful openthe error state is unchanged
      LWG 409C++98the error state was unchanged upon a successful openit is cleared[1]
      LWG 460C++98the default argument ofmode in overload(1)
      was missing (it is present in thesynopsis)
      added
      1. The resolution of LWG issue #22 is overridden.

      [edit]See also

      checks if the stream has an associated file
      (public member function)[edit]
      closes the associated file
      (public member function)[edit]
      opens a file and configures it as the associated character sequence
      (public member function ofstd::basic_filebuf<CharT,Traits>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_fstream/open&oldid=147681"

      [8]ページ先頭

      ©2009-2025 Movatter.jp