std::basic_...sic_ofstream(3)
β¨ NAME β¨
std::basic_ofstream::basic_ofstream β std::basic_ofstream::basic_ofstream, bestie. That's the name and the one-liner, and this is the std::basic_...sic_ofstream(3) page from the C++ Standard Libary manual. We're talking about the constructor today, and it's honestly kind of iconic. π
πΈ Synopsis πΈ
Here are the overloads, exactly as they appear β no cap:
basic_ofstream();
explicit basic_ofstream( const char* filename, std::ios_base::openmode mode = std::ios_base::out );
explicit basic_ofstream( const std::filesystem::path::value_type* filename, std::ios_base::openmode mode = std::ios_base::out );
explicit basic_ofstream( const std::string& filename, std::ios_base::openmode mode = std::ios_base::out );
template< class FsPath >
explicit basic_ofstream( const FsPath& filename, std::ios_base::openmode mode = std::ios_base::out );
basic_ofstream( basic_ofstream&& other );
basic_ofstream( const basic_ofstream& rhs ) = delete;
Quick vibe check on the numbers: (1) is the default one, (2) is the const char* one, (3) is the std::filesystem::path::value_type* one and it's (since C++17), (4) is the std::string& one and it's (since C++11), (5) is the FsPath template one and it's (since C++17), (6) is the move constructor and it's (since C++11), and (7) is the deleted copy constructor and it's (since C++11). π
Constructs new file stream. Here's the tea, overload by overload:
- Overload (1) β default constructor: constructs a stream that is not associated with a file. It default-constructs the
std::basic_filebufand constructs the base with the pointer to this default-constructedstd::basic_filebufmember. π΄ - Overload (2) β first, performs the same steps as the default constructor, then associates the stream with a file by calling
rdbuf()->open(filename, mode | std::ios_base::out)(seestd::basic_filebuf::openfor the details on the effects of that call). If theopen()call returns a null pointer, setssetstate(failbit). π οΈ - Overload (3) β same as (2), and it's only provided if
std::filesystem::path::value_typeis notcharβ(since C++17). π - Overload (4) β same as
basic_ofstream(filename.c_str(), mode)β(since C++11). π - Overload (5) β same as
basic_ofstream(filename.c_str(), mode), and it participates in overload resolution only ifFsPathisstd::filesystem::pathβ(since C++17).
Also, ngl, despite the default mode beingout, the effects are identical to the effects ofout | truncas described instd::filebuf::open. π
- Overload (6) β move constructor: first, move-constructs the base class from
other(which does not affect therdbuf()pointer), then move-constructs thestd::basic_filebufmember, then callsthis->set_rdbuf()to install the newbasic_filebufas therdbuf()pointer in the base class. π - Overload (7) β the copy-constructor is deleted: this class is not copyable. periodt. π«
π Parameters π
filenameβ the name of the file to be opened. ποΈmodeβ specifies stream open mode. The following constants and bit-wise OR between them may be used:appβ seek to the end of stream before each writebinaryβ open in binary modeinβ open for readingoutβ open for writingtruncβ discard the contents of the stream when openingateβ seek to the end of stream immediately after opennoreplace (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));
}
This example is giving "I can construct file streams in my sleep" energy, and it's literally the whole code block. π
π©Ή Defect reports π©Ή
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR:
LWG 3430β Applied to:C++17. Behavior as published:std::filesystem::pathoverload led to unwanted conversions. Correct behavior: avoided by making it a template.
π 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 ofstd::basic_filebuf<CharT,Traits>)set_rdbufβ replaces therdbufwithout clearing its error state (protected member function)constructorβ constructs the object (public member function ofstd::basic_ostream<CharT,Traits>)
okay bestie, that's the whole page π source: http://cppreference.com β version/date: 2024.06.10 β page: std::basic_...sic_ofstream(3). bye! π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms