名稱空間
變體
操作

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

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

檢查檔案流是否關聯了檔案。

實際呼叫 rdbuf()->is_open()

目錄

[編輯] 引數

(無)

[編輯] 返回值

如果檔案流關聯了檔案,則為 true,否則為 false

[編輯] 示例

#include <fstream>
#include <iostream>
#include <string>
 
// this file is called main.cpp
 
bool file_exists(const std::string& str)
{
    std::ifstream fs(str);
    return fs.is_open();
}
 
int main()
{
    std::boolalpha(std::cout);
    std::cout << file_exists("main.cpp")  << '\n'
              << file_exists("strange_file") << '\n';
}

可能的輸出

true
false

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 365 C++98 is_open 未用 const 限定符宣告 const 限定符宣告

[編輯] 參閱

開啟檔案並將其與流關聯
(public member function) [編輯]
關閉關聯的檔案
(public member function) [編輯]