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

相关推荐
Moweiii2 分钟前
SDL3 GPU编程探索
c++·游戏引擎·图形渲染·sdl·vulkan
渝妳学C30 分钟前
【C++】类和对象(下)
c++
数据小小爬虫31 分钟前
Python爬虫获取AliExpress商品详情
开发语言·爬虫·python
小爬虫程序猿31 分钟前
利用Python爬虫速卖通按关键字搜索AliExpress商品
开发语言·爬虫·python
一朵好运莲38 分钟前
React引入Echart水球图
开发语言·javascript·ecmascript
EleganceJiaBao44 分钟前
【C语言】结构体模块化编程
c语言·c++·模块化·static·结构体·struct·耦合
Eiceblue1 小时前
使用Python获取PDF文本和图片的精确位置
开发语言·python·pdf
xianwu5431 小时前
反向代理模块。开发
linux·开发语言·网络·c++·git
xiaocaibao7771 小时前
Java语言的网络编程
开发语言·后端·golang
Bucai_不才1 小时前
【C++】初识C++之C语言加入光荣的进化(上)
c语言·c++·面向对象