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++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 365 | C++98 | is_open 未用 const 限定符声明 |
用 const 限定符声明 |
[编辑] 参阅
打开文件并将其与流关联 (public member function) | |
关闭关联的文件 (public member function) |