std::basic_ifstream<CharT,Traits>::is_open
来自cppreference.com
< cpp | io | basic ifstream
输入/输出库
std::basic_ifstream
成员函数 | ||||
(C++11)
|
||||
(C++11)
|
||||
文件操作 | ||||
basic_ifstream::is_open
|
||||
非成员函数 | ||||
(C++11)
|
bool
is_open() const;
|
||
检查文件流是否有关联文件。
与调用 rdbuf()->is_open() 的效果相同。
参数
(无)
返回值
文件流有关联文件时返回 true,否则返回 false。
示例
运行此代码
#include <string> #include <fstream> #include <iostream> // 此文件名为 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'; }
可能的输出:
true false
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
---|---|---|---|
LWG 365 | C++98 | is_open 的声明不带 const 限定符
|
带 const 限定符 |
参阅
打开文件,并将它与流关联 (公开成员函数) |
|
关闭关联文件 (公开成员函数) |