Namespaces
Variants

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

From cppreference.net

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_open const 修飾子で宣言されていなかった const 修飾子で宣言される

関連項目

ファイルを開き、ストリームに関連付ける
(公開メンバ関数)
関連付けられたファイルを閉じる
(公開メンバ関数)