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

std::basic_fstream::close

void close();

关闭关联文件。

有效呼叫rdbuf()->close()如果操作过程中发生错误,setstate(failbit)叫做。

参数

%280%29

返回值

%280%29

注记

此函数由Basic的析构函数调用。[医]当流对象超出作用域时,通常不会直接调用fstream。

二次

代码语言:javascript
复制
#include <string>
#include <fstream>
#include <iostream>
 
int main()
{
    std::fstream f1("example1", std::ios::out),
                 f2("example2", std::ios::out),
                 f3("example3", std::ios::out);
 
    std::cout << std::boolalpha
              << f1.is_open() << '\n'
              << f2.is_open() << '\n'
              << f3.is_open() << '\n';
 
    f1.close();
    f2.close();
 
    std::cout << f1.is_open() << '\n'
              << f2.is_open() << '\n'
              << f3.is_open() << '\n';
}

二次

可能的产出:

二次

代码语言:javascript
复制
true
true
true
false
false
true

二次

另见

is_open

checks if the stream has an associated file (public member function)

open

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

close

flushes the put area buffer and closes the associated file (public member function of std::basic_filebuf)

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

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

扫码关注腾讯云开发者

领取腾讯云代金券