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.
對同一的併發訪問可能會導致資料競爭。

異常安全

強異常保證:如果丟擲異常,不會發生更改。

另見