std::basic_fstream::open(3)

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

πŸ’… std::basic_fstream::open(3) πŸ’…

okay so we're on the C++ Standard Libary shelf of the manual (spelled exactly like that in the header, no cap 😭🫢). This page is all about std::basic_fstream::open, and it's basically the "attach this file stream to a file" moment. iconic. ✨

🌸 NAME 🌸

std::basic_fstream::open β€” std::basic_fstream::open (yes bestie, the NAME section really said it twice, and honestly? slay πŸ’…).

✨ Synopsis ✨

Here are the overloads, defaults included. Do not lose the defaults, they are lowkey the whole vibe:

          void open( const char* filename,

                     std::ios_base::openmode mode                       (1)

                         = std::ios_base::in | 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::in | std::ios_base::out );
          void open( const std::string& filename,

                     std::ios_base::openmode mode                       (3)
       (since C++11)

                         = std::ios_base::in | std::ios_base::out );
          void open( const std::filesystem::path& filename,

                     std::ios_base::openmode mode                       (4)
       (since C++17)

                         = std::ios_base::in | std::ios_base::out );

This opens the file with name filename and associates it with the file stream. On success it calls clear(). On failure it calls setstate(failbit). 🫢

  • Overloads (1) and (2): effectively call rdbuf()->open(filename, mode) β€” 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)
  • Overloads (3) and (4): effectively call (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; 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, bestie. That's the whole answer. πŸ’€

πŸ¦‹ Example πŸ¦‹

       // Run this code

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

        int main()
        {
            std::string filename = "example.123";

            std::fstream fs;
            fs.open(filename);

            if (!fs.is_open())
            {
                fs.clear();
                fs.open(filename, std::ios::out); // create file
                fs.close();
                fs.open(filename);
            }

            std::cout << std::boolalpha;
            std::cout << "fs.is_open() = " << fs.is_open() << '\n';
            std::cout << "fs.good() = " << fs.good() << '\n';
        }

πŸ”₯ Defect reports πŸ”₯

These behavior-changing defect reports were applied retroactively to previously published C++ standards, and yes they ate:

  • 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].
  • DR LWG 460 β€” Applied to: C++98. Behavior as published: the default argument of mode in overload (1) was missing (it is present in the synopsis). Correct behavior: added.
  • ^[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>)

okay that's the whole page, bestie! cute little footer tea for you: source/version is http://cppreference.com, date is 2024.06.10, and the page is std::basic_fstream::open(3). bye πŸ’–πŸ¦‹βœ¨

πŸ“¦ where this page comes from