Namespaces
Variants

std::basic_ifstream<CharT,Traits>:: is_open

From cppreference.net

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 限定符声明

参见

打开文件并将其与流关联
(公开成员函数)
关闭关联的文件
(公开成员函数)