std::basic_fstream::open(3)
π 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)β seestd::basic_filebuf::openfor the details on the effects of that call. Overload (2) is only provided ifstd::filesystem::path::value_typeis notchar. (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 writebinaryβ open in binary modeinβ open for readingoutβ open for writingtruncβ discard the contents of the stream when openingateβ seek to the end of stream immediately after opennoreplace(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 ofmodein 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 ofstd::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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms