std::ios_base::openmode(3)

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

πŸ’– NAME

Okay bestie, welcome to std::ios_base::openmode(3) β€” the page from the C++ Standard Libary manual. The name section is literally std::ios_base::openmode - std::ios_base::openmode, and honestly? iconic repetitive queen behavior πŸ’….

✨ Synopsis

Here's the whole typedef-and-constants situation, copied exactly so you can trust it:

typedef /* implementation defined */ openmode;
static constexpr openmode app       = /* implementation defined */;

static constexpr openmode binary    = /* implementation defined */;
static constexpr openmode in        = /* implementation defined */;
static constexpr openmode out       = /* implementation defined */;
static constexpr openmode trunc     = /* implementation defined */;

static constexpr openmode ate       = /* implementation defined */;
static constexpr openmode noreplace = /* implementation defined */;
(since C++23)

So openmode is a type that specifies the available file open flags. It's a BitmaskType, and the following constants are defined β€” no cap:

  • 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 β€” open in exclusive mode; this one is (since C++23) so it's the shiny new-ish bestie on the block ✨

πŸŽ€ Example

The page literally says // Run this code right before this, so here is the exact example:

#include <fstream>
#include <iostream>
#include <string>

int main()
{
    const char* fname = "unique_name.txt";

    // write to a temporary stream object
    std::fstream(fname, std::ios::out | std::ios::trunc) << "Hi";

    std::string s;
    std::fstream(fname, std::ios::in) >> s;
    std::cout << s << '\n';
}

🌸 Output

Hi

πŸ¦‹ See also

  • open β€” opens a file and configures it as the associated character sequence (public member function of std::basic_filebuf<CharT,Traits>)
  • constructor β€” constructs a basic_stringbuf object (public member function of std::basic_stringbuf<CharT,Traits,Allocator>)

Cute lil sign-off: this page came from source http://cppreference.com, version/date 2024.06.10, and it's the std::ios_base::openmode(3) page. Bye bestie, go open some streams responsibly πŸ«ΆπŸ’–

πŸ“¦ where this page comes from