public member function
<fstream>

std::basic_fstream::is_open

bool is_open();
bool is_open() const;
检查文件是否已打开
返回流当前是否与文件关联。

Streams can be associated to files by a successful call to member open or directly on construction, and disassociated by calling close or on destruction.

流的文件关联由其内部流缓冲区维护
Internally, the function calls rdbuf()->is_open()

参数



返回值

如果文件已打开并与此对象关联,则为true
否则返回 false

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// fstream::is_open
#include <iostream>     // std::cout
#include <fstream>      // std::fstream

int main () {
  std::fstream fs;
  fs.open ("test.txt");
  if (fs.is_open())
  {
    fs << "lorem ipsum";
    std::cout << "Operation successfully performed\n";
    fs.close();
  }
  else
  {
    std::cout << "Error opening file";
  }
  return 0;
}

可能的输出
Operation successfully performed


数据竞争

Accesses the basic_fstream object.
对同一的并发访问可能会导致数据竞争。

异常安全

强异常保证:如果抛出异常,不会发生更改。

另见