std::basic_ifstream<CharT,Traits>::is_open
来自cppreference.com
| |
||
检查文件流是否有关联文件。
相当于调用 rdbuf()->is_open()。
参数
(无)
返回值
文件流有关联文件时返回 true,否则返回 false。
示例
运行此代码
#include <fstream>
#include <iostream>
#include <string>
// 此文件名为 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 限定符
|
参阅
| 打开文件,并将它与流关联 (公开成员函数) | |
| 关闭关联文件 (公开成员函数) |