std::basic_ifstream::is_open
bool is_open(); | | (until C++11) |
|---|---|---|
bool is_open() const; | | (since C++11) |
检查文件流是否有关联的文件。
有效呼叫rdbuf()->is_open()...
参数
%280%29
返回值
true如果文件流有关联的文件,false否则。
例
二次
#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二次
另见
open | opens a file and associates it with the stream (public member function) |
|---|---|
close | closes the associated file (public member function) |
© cppreference.com在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

