public 成員函式
<fstream>

std::ofstream::is_open

bool is_open();
bool is_open() const;
檢查檔案是否已開啟
返回流當前是否與檔案關聯。

透過成功呼叫成員函式 open 或在構造時關聯檔案,並透過呼叫 close 或在析構時解除檔案關聯。

流的檔案關聯由其內部流緩衝區維護
內部呼叫函式 rdbuf()->is_open()

引數



返回值

如果檔案已開啟並與此物件關聯,則為true
否則返回 false

示例

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

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

可能的輸出
Output operation successfully performed


資料競爭

訪問 ofstream 物件。
對同一的併發訪問可能會導致資料競爭。

異常安全

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

另見