名前空間
変種
操作

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

From 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++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 365 C++98 is_openconst 修飾子なしで宣言されていました。 const 修飾子付きで宣言されています。

[編集] 関連項目

ファイルを開き、ストリームに関連付ける
(public member function) [編集]
関連付けられたファイルを閉じる
(public member function) [編集]
English 日本語 中文(简体) 中文(繁體)