home | help
std::basic_...sic_ofstream(3)  C++ Standard Libary std::basic_...sic_ofstream(3)

NAME
     std::basic_ofstream::basic_ofstream - std::basic_ofstream::basic_ofstream

Synopsis
	basic_ofstream();						   (1)
	explicit basic_ofstream( const char* filename,

				 std::ios_base::openmode mode		   (2)

				     = std::ios_base::out );
	explicit basic_ofstream( const std::filesystem::path::value_type*
	filename,
									   (3)
     (since C++17)
				 std::ios_base::openmode mode

				     = std::ios_base::out );
	explicit basic_ofstream( const std::string& filename,

				 std::ios_base::openmode  mode		     (4)
     (since C++11)

				     = std::ios_base::out );
	template< class FsPath >

	explicit basic_ofstream( const FsPath&	filename,		     (5)
     (since C++17)
				 std::ios_base::openmode mode

				     = std::ios_base::out );
	basic_ofstream(  basic_ofstream&&  other );			     (6)
     (since C++11)
	basic_ofstream( const basic_ofstream& 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 the std::basic_filebuf and constructs the  base  with
     the pointer
	to this default-constructed std::basic_filebuf member.
	2,3) First, performs the same steps as the default constructor, then as-
     sociates the
	stream	 with	a   file   by  calling	rdbuf()->open(filename,  mode  |
     std::ios_base::out)
	(see std::basic_filebuf::open for the details on  the  effects	of  that
     call). If the
	open() call returns a null pointer, sets setstate(failbit).
	Overload  (3)  is  only provided if std::filesystem::path::value_type is
     not char.
	(since C++17)
	4,5) Same as basic_ofstream(filename.c_str(), mode).
	(5) participates in overload resolution only if FsPath is  std::filesys-
     tem::path.
	(since	C++17) Note that despite the default mode being out, the effects
     are
	identical to the effects of out  |  trunc  as  described  in  std::file-
     buf::open
	6)  Move  constructor.	First, move-constructs the base class from other
     (which does
	not affect the	rdbuf()  pointer),  then  move-constructs  the	std::ba-
     sic_filebuf member,
	then calls this->set_rdbuf() to install the new basic_filebuf as the rd-
     buf() pointer
	in the base class.
	7) The copy-constructor is deleted: this class is not copyable.

Parameters
	filename - the name of the file to be opened
		   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
	mode	 - binary	     open in binary 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

Example
     // Run this code

      #include <fstream>
      #include <string>
      #include <utility>

      int main()
      {
	  std::ofstream f0;
	  std::ofstream f1("test.bin", std::ios::binary);
	  std::string name = "example.txt";
	  std::ofstream f2(name);
	  std::ofstream f3(std::move(f1));
      }

	Defect reports

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

	   DR	 Applied to	       Behavior as  published		    Cor-
     rect behavior
	LWG  3430 C++17      std::filesystem::path overload led to	 avoided
     by making it
			    unwanted conversions			 a  tem-
     plate

See also
	open	      opens a file and associates it with the stream
		      (public member function)
	open	      opens a file and configures it as the associated character
     sequence
		      (public	  member     function	  of	std::basic_file-
     buf<CharT,Traits>)
	set_rdbuf     replaces the rdbuf without clearing its error state
		      (protected member function)
	constructor   constructs the object
		      (public	  member     function	  of	  std::basic_os-
     tream<CharT,Traits>)

http://cppreference.com 	   2024.06.10	   std::basic_...sic_ofstream(3)

home | help