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

来自cppreference.com
< cpp‎ | io‎ | basic filebuf
 
 
 
 
bool is_open() const;

若最近对 open() 的调用成功且之后无对 close() 的调用则返回 true

参数

(无)

返回值

若关联文件打开则为 true,否则为 false

注解

此函数通常被 std::basic_fstream::is_open() 调用。

示例

#include <fstream>
#include <iostream>
 
int main()
{
    std::ifstream fs("test.txt");
    std::filebuf fb;
    fb.open("test.txt", std::ios_base::in);
    std::cout << std::boolalpha
              << "直接调用: " << fb.is_open() << '\n'
              << "通过 streambuf: " << fs.rdbuf()->is_open() << '\n'
              << "通过 fstream: " << fs.is_open() << '\n';
}

输出:

直接调用: true
通过 streambuf: true
通过 fstream: true

参阅

打开文件并配置它为关联字符序列
(公开成员函数)
冲洗放置区缓冲区并关闭关联的文件
(公开成员函数)