std::basic_...asic_fstream(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– NAME πŸ’–

okay so you're on the std::basic_...asic_fstream(3) page in the C++ Standard Libary manual (yes, spelling exactly as the header gives it πŸ’…), and the main character is:

std::basic_fstream::basic_fstream β€” std::basic_fstream::basic_fstream


✨ Synopsis ✨

bestie, this is the constructor overload set, and there are seven of them. stay with me 🫢

basic_fstream();

(1)

explicit basic_fstream( const char* 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( const std::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. That's the whole assignment, but the details are giving very much β€œread the fine print” energy. 🌸

  1. Default constructor β€” constructs a stream that is not associated with a file: it default-constructs the std::basic_filebuf and constructs the base with the pointer to this default-constructed std::basic_filebuf member.
  2. Overloads (2), (3) β€” first, performs the same steps as the default constructor, then associates the stream with a file by calling rdbuf()->open(filename, mode) (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)
  3. Overloads (4), (5) β€” same as basic_fstream(filename.c_str(), mode). Overload (5) participates in overload resolution only if FsPath is std::filesystem::path. (since C++17)
  4. Overload (6) β€” move constructor. First, move-constructs the base class from other (which does not affect the rdbuf() pointer), then move-constructs the std::basic_filebuf member, then calls this->set_rdbuf() to install the new basic_filebuf as the rdbuf() pointer in the base class.
  5. Overload (7) β€” the copy-constructor is deleted: this class is not copyable. no duplicating, bestie 🚫

πŸŽ€ 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:
    • app β€” seek to the end of stream before each write
    • 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::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));
}

πŸ¦‹ Defect reports πŸ¦‹

The following behavior-changing defect reports were applied retroactively to previously published C++ standards. history really said β€œlet me fix that for you” πŸ’€

  • LWG 460 β€” applied to C++98. Behavior as published: the default argument of mode in overload (2) was missing (it is present in the synopsis). Correct behavior: added.
  • LWG 3430 β€” applied to C++17. Behavior as published: std::filesystem::path overload 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 of std::basic_filebuf<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_iostream<CharT,Traits>).

source/version + date sign-off, bestie: http://cppreference.com Β· 2024.06.10 Β· std::basic_...asic_fstream(3) β€” that's the footer, and we're done. periodt. πŸ’œ

πŸ“¦ where this page comes from