std::basic_ifstream<CharT,Traits>::is_open
提供: cppreference.com
bool is_open(); |
(C++11未満) | |
bool is_open() const; |
(C++11以上) | |
ファイルストリームが紐付けられているファイルを持っているかどうか調べます。
実質的に rdbuf()->is_open() を呼びます。
引数
(なし)
戻り値
ファイルストリームが紐付けられているファイルを持っている場合は true、そうでなければ false。
例
Run this code
#include <string>
#include <fstream>
#include <iostream>
//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
関連項目
| ファイルを開き、それをストリームと紐付けます (パブリックメンバ関数) | |
| 紐付けられているファイルを閉じます (パブリックメンバ関数) |