std::basic_ifstream::open(3)

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

✨ std::basic_ifstream::open ✨

Bestie, welcome to the std::basic_ifstream::open page from the C++ Standard Libary manual πŸ’– This is the vibe: opening a file and associating it with your file stream. It's giving file-connection main character energy. πŸŽ€

🌸 NAME 🌸

std::basic_ifstream::open β€” std::basic_ifstream::open

Okay yes, the NAME section literally says it twice, and we respect the icon behavior. No cap. πŸ’…

πŸ¦‹ Synopsis πŸ¦‹

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

Okay so this opens the file with name filename and associates it with the file stream. Slay. 🫢

On success it calls clear(). On failure it calls setstate(failbit). Periodt.

  • 1, 2) Effectively calls rdbuf()->open(filename, mode | ios_base::in) (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).

🌈 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

πŸ“ Return value πŸ“

(none) β€” literally nothing comes back, bestie. 🫢

πŸ’€ Example πŸ’€

This section is incomplete.

Reason: no example 😭

😭 Defect reports 😭

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

  • 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.
  • 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

mwah πŸ’‹ this iconic page came from http://cppreference.com, dated 2024.06.10, and it's the std::basic_ifstream::open(3) page β€” bye bestie! 🫢

πŸ“¦ where this page comes from