首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::basic_fstream::is_open

bool is_open();

?

(until C++11)

bool is_open() const;

?

(since C++11)

检查文件流是否有关联的文件。

有效呼叫rdbuf()->is_open()...

参数

%280%29

返回值

true如果文件流有关联的文件,false否则。

二次

代码语言:javascript
复制
#include <string>
#include <fstream>
#include <iostream>
 
int main()
{
    std::string filename = "some_file";
 
    std::fstream fs(filename, std::ios::in);
 
    std::cout << std::boolalpha;
    std::cout << "fs.is_open() = " << fs.is_open() << '\n';
 
    if(!fs.is_open())
    {
       fs.clear();
       fs.open(filename, std::ios::out);
       std::cout << "fs.is_open() = " << fs.is_open() << '\n';
    }
}

二次

可能的产出:

二次

代码语言:javascript
复制
fs.is_open() = false
fs.is_open() = true

二次

另见

open

opens a file and associates it with the stream (public member function)

close

closes the associated file (public member function)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com