ofstream是否会自动关闭其文件? [英] Does ofstream close its files automatically?

查看:434
本文介绍了ofstream是否会自动关闭其文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
我需要关闭std :: fstream吗?

Possible Duplicate:
do I need to close a std::fstream?

int main() {
    ofstream a("a.txt");
    a << "A" << endl;
    //a.close();
}

这很好用,但是在程序结束时不是必须关闭文件吗?

This works fine, but isn't it necessary to close the file at the end of the program?

推荐答案

ofstream将在调用其析构函数(即超出范围)时关闭文件.但是,调用close()肯定不会造成任何危害,并向维护程序员表达了您的意图.

ofstream will close files when its destructor is called, i.e. when it goes out of scope. However, calling close() certainly doesn't do any harm and expresses your intentions to maintenance programmers.

调用close()还可让您检查close()是否成功,因为随后您还可以检查failbit:

Calling close() also allows you to check if the close() was successful because you can then also check the failbit:

http://www.cplusplus.com/reference/iostream/ofstream/close /

这篇关于ofstream是否会自动关闭其文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆