std::basic_...am::is_open(3)
β¨ NAME β¨
std::basic_ifstream::is_open β bestie, it's literally std::basic_ifstream::is_open π
(this is a page from the C++ Standard Library manual, btw β keeping you informed π«Ά)
πΈ SYNOPSIS πΈ
okay so here's the whole vibe, no cap:
bool is_open() const;
What it does? Checks if the file stream has an associated file. That's it. That's the function. π
Under the hood it's effectively calling rdbuf()->is_open(), so like, it's not doing anything magical β it's just asking its buddy rdbuf() to do the actual checking. iykyk β¨
π¦ PARAMETERS π¦
- (none) β bestie takes zero arguments, she needs NOTHING from you π
β RETURN VALUE β
trueβ if the file stream has an associated file πfalseβ otherwise π
that's the whole thing. very demure. very mindful. π
π₯ EXAMPLE π₯
// Run this code
#include <fstream>
#include <iostream>
#include <string>
// this file is called main.cpp
bool file_exists(const std::string& str)
{
std::ifstream fs(str);
return fs.is_open();
}
int main()
{
std::boolalpha(std::cout);
std::cout << file_exists("main.cpp") << '\n'
<< file_exists("strange_file") << '\n';
}
okay so notice std::boolalpha(std::cout) β that's the main character move that makes it print true/false instead of 1/0. obsessed. π
πΈ Possible output πΈ
true
false
like, main.cpp exists (obviously, it's right there), and strange_file... does not. the audacity honestly π
π DEFECT REPORTS π
so there were these behavior-changing defect reports that got applied retroactively to previously published C++ standards β which is lowkey wild when you think about it, they went back and fixed the past π
Here's the tea:
- DR:
LWG 365 - Applied to: C++98
- Behavior as published:
is_openwas not declared withconstqualifier - Correct behavior: declared with
constqualifier
so yeah, it got that const glow-up and honestly she deserved it πβ¨
π§ SEE ALSO π§
openβ opens a file and associates it with the stream (public member function) π¦closeβ closes the associated file (public member function) π
and that's the page bestie!! go check your streams, stay hydrated, and remember: if is_open() returns false, it's not you, it's the file path ππ
π courtesy of http://cppreference.com β version 2024.06.10 β slay safe out there πβ¨
π¦ 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