std::basic_ofstream::open(3)

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

πŸ’– NAME πŸ’–

std::basic_ofstream::open - std::basic_ofstream::open

okay so this page is std::basic_ofstream::open from the C++ Standard Libary manual (yes, that's literally how the header spells it 😭), and it's lowkey the moment your output file stream gets a file bestie to talk to. ✨

✨ Synopsis ✨

void open( const char* filename,
           std::ios_base::openmode mode                       (1)
               = std::ios_base::out );
void open( const std::filesystem::path::value_type* filename,
           std::ios_base::openmode mode                       (2)
       (since C++17)
               = std::ios_base::out );
void open( const std::string& filename,
           std::ios_base::openmode mode                       (3)
       (since C++11)
               = std::ios_base::out );
void open( const std::filesystem::path& filename,
           std::ios_base::openmode mode                       (4)
       (since C++17)
               = std::ios_base::out );

πŸŽ€ Parameters πŸŽ€

  • filename β€” the name of the file to be opened πŸ’…
  • mode β€” specifies stream open mode. It is a BitmaskType, and the following constants are defined:
    • 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

πŸ¦‹ Description πŸ¦‹

so the whole vibe is: opens and associates the file with name filename with the file stream. that's the main character move. 🌟

then the important little detail, no cap: calls clear() on success. calls setstate(failbit) on failure. 🫢

here's the overload breakdown, bestie:

  • 1,2) Effectively calls rdbuf()->open(filename, mode | ios_base::out) (see std::basic_filebuf::open for the details on the effects of that call).
  • Overload (2) is only provided if std::filesystem::path::value_type is not char. (since C++17)
  • 3,4) Effectively calls (1,2) as if by open(filename.c_str(), mode).

🌸 Return value 🌸

(none) β€” that's it, that's the return value. iconic minimalism. πŸ’€

πŸ“ Example πŸ“

This section is incomplete.

Reason: no example.

πŸ’… Defect reports πŸ’…

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

  • DR LWG 22 β€” Applied to: C++98; Behavior as published: it was unclear how error state changes upon a successful open; Correct behavior: the error state is unchanged.
  • DR LWG 409 β€” Applied to: C++98; Behavior as published: the error state was unchanged upon a successful open; Correct behavior: it is cleared^[1].

1. ↑ The resolution of LWG issue #22 is overriden.

πŸ‘‘ See also πŸ‘‘

  • is_open β€” checks if the stream has an associated file (public member function).
  • close β€” closes the associated file (public member function).
  • open β€” opens a file and configures it as the associated character sequence (public member function of std::basic_filebuf<CharT,Traits>).

πŸ“ Category πŸ“

  • * Todo no example

bye bestie πŸ’– β€” source: http://cppreference.com; version/date: 2024.06.10; page: std::basic_ofstream::open(3).

πŸ“¦ where this page comes from