redies&&

std::ofstream::is_open

C++98C++11

bool is_open();

Check if file is open

Returns whether the stream is currently associated to a file.

Streams can be associated to files by a successful call to member open or directly on construction, and disassociated by calling close or on destruction.

The file association of a stream is kept by its internal stream buffer:

Internally, the function calls rdbuf()->is_open()

Parameters

none

Return Value

true if a file is open and associated with this stream object.

false otherwise.

Example

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

// ofstream::is_open

#include // std::cout

#include // std::ofstream

int main () {

std::ofstream ofs;

ofs.open ("test.txt");

if (ofs.is_open())

{

ofs << "lorem ipsum";

std::cout << "Output operation successfully performed\n";

ofs.close();

}

else

{

std::cout << "Error opening file";

}

return 0;

}

Edit & Run

Possible output:

Output operation successfully performed

Data races

Accesses the ofstream object.

Concurrent access to the same stream may introduce data races.

Exception safety

相关推荐
张晓~1833994812115 分钟前
碰一碰发视频 系统源码 /PHP 语言开发方案
开发语言·线性代数·矩阵·aigc·php·音视频·文心一言
代码不停17 分钟前
Java前缀和算法题目练习
java·开发语言·算法
豆沙沙包?17 分钟前
2025年--Lc200- 414. 第三大的数(大根堆)--Java版
java·开发语言
一念&24 分钟前
每日一个C语言知识:C 指针
c语言·开发语言
涤生z25 分钟前
list.
开发语言·数据结构·c++·学习·算法·list
xxxxxxllllllshi29 分钟前
Java中Elasticsearch完全指南:从零基础到实战应用
java·开发语言·elasticsearch·面试·职场和发展·jenkins
FFZero140 分钟前
【C++/Lua联合开发】 (三) C++调用Lua
c++·junit·lua
wu_jing_sheng042 分钟前
Python中使用HTTP 206状态码实现大文件下载的完整指南
开发语言·前端·python
Source.Liu1 小时前
【BuildFlow & 筑流】品牌命名与项目定位说明
c++·qt·rust·markdown·librecad
bkspiderx1 小时前
C++设计模式之行为型模式:访问者模式(Visitor)
c++·设计模式·访问者模式