public member function
<fstream>

std::ifstream::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
20
21
22
// ifstream::is_open
#include <iostream>     // std::cout
#include <fstream>      // std::ifstream

int main () {
  std::ifstream ifs ("test.txt");

  if (ifs.is_open()) {
    // print file:
    char c = ifs.get();
    while (ifs.good()) {
      std::cout << c;
      c = ifs.get();
    }
  }
  else {
    // show message:
    std::cout << "Error opening file";
  }

  return 0;
}

数据竞争

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

异常安全

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

另见