Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |io‎ |basic filebuf
       
       
       
       
      basic_filebuf* open(constchar* s,std::ios_base::openmode mode);
      (1)
      basic_filebuf* open(conststd::string& str,std::ios_base::openmode mode);
      (2)(since C++11)
      basic_filebuf* open(conststd::filesystem::path& p,
                           std::ios_base::openmode mode);
      (3)(since C++17)
      basic_filebuf* open(const std::filesystem::path::value_type* s,
                           std::ios_base::openmode mode);
      (4)(since C++17)

      If the associated file was already open (is_open()!=false), returns a null pointer right away.

      Otherwise, opens the file with the given name (s,p.c_str()(since C++17) orstr.c_str(), depending on the overload).std::ios_base::openmode values may be written as, e.g.,std::ios_base::out|std::ios_base::app.

      Overload(4) is only provided ifstd::filesystem::path::value_type is notchar.

      (since C++17)

      The file is opened as if by callingstd::fopen with the second argument (file access mode) determined by the result ofmode& ~std::ios_base::ate as follows,open() fails if the result is not some combination of flags shown in the table:

      mode& ~std::ios_base::ate std::fopen 
      access
      mode
      Action if file already existsAction if file does not exist
      binaryinouttruncappnoreplace
      (since C++23)
      -+ - - - -"r"Read from startFailure to open
      ++ - - - -"rb"
      -++ - - -"r+"Error
      +++ - - -"r+b"
      - -+ - - -"w"Destroy contentsCreate new
      - -++ - -
      + -+ - - -"wb"
      + -++ - -
      -+++ - -"w+"
      ++++ - -"w+b"
      - -+ - -+"wx"Failure to openCreate new
      - -++ -+
      + -+ - -+"wbx"
      + -++ -+
      -+++ -+"w+x"
      ++++ -+"w+bx"
      - -+ -+ -"a"Write to endCreate new
      - - - -+ -
      + -+ -+ -"ab"
      + - - -+ -
      -++ -+ -"a+"
      -+ - -+ -
      +++ -+ -"a+b"
      ++ - -+ -

      If the open operation succeeds and(openmode&std::ios_base::ate)!=0 (theate bit is set), repositions the file position to the end of file, as if by callingstd::fseek(file,0,SEEK_END), wherefile is the pointer returned by callingstd::fopen. If the repositioning fails, callsclose() and returns a null pointer to indicate failure.

      Contents

      [edit]Parameters

      s, str, p - the file name to open;s must point to a null-terminated string
      openmode - the file opening mode, a binary OR of thestd::ios_base::openmode modes

      [edit]Return value

      this on success, a null pointer on failure.

      [edit]Notes

      open() is typically called through the constructor or theopen() member function ofstd::basic_fstream.

      [edit]Example

      Run this code
      #include <fstream>#include <iostream> int main(){std::string filename="Test.b";std::filebuf fb; // prepare a file to readdouble d=3.14;if(!fb.open(filename, std::ios::binary| std::ios::out)){std::cout<<"Open file "<< filename<<" for write failed\n";return1;}     fb.sputn(reinterpret_cast<char*>(&d), sizeof d);    fb.close(); // open file for readingdouble d2=0.0;if(!fb.open(filename, std::ios::binary| std::ios::in)){std::cout<<"Open file "<< filename<<" for read failed\n";return1;} auto got= fb.sgetn(reinterpret_cast<char*>(&d2), sizeof d2);if(sizeof(d2)!= got)std::cout<<"Read of "<< filename<<" failed\n";elsestd::cout<<"Read back from file: "<< d2<<'\n';}

      Output:

      Read back from file: 3.14

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 596C++98open() could not open files in append modecan open in append mode

      [edit]See also

      checks if the associated file is open
      (public member function)[edit]
      flushes the put area buffer and closes the associated file
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_filebuf/open&oldid=155280"

      [8]ページ先頭

      ©2009-2025 Movatter.jp