std::basic_ifstream<CharT,Traits>::is_open
来自 cppreference.cn
< cpp | io | basic ifstream
bool is_open() const; |
||
检查文件流是否有关联的文件。
实际调用 rdbuf()->is_open()。
目录 |
[编辑] 参数
(无)
[编辑] 返回值
true 如果文件流有关联的文件,则为 false 否则为否。
[编辑] 示例
运行此代码
#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'; }
可能的输出
true false
[编辑] 缺陷报告
以下行为更改缺陷报告已追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 365 | C++98 | is_open 未使用 const 限定符声明 |
使用 const 限定符声明 |
[编辑] 参见
打开文件并将其与流关联 (公共成员函数) | |
关闭关联的文件 (公共成员函数) |