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

相关推荐
奋斗的小花生2 分钟前
c++ 多态性
开发语言·c++
魔道不误砍柴功4 分钟前
Java 中如何巧妙应用 Function 让方法复用性更强
java·开发语言·python
闲晨7 分钟前
C++ 继承:代码传承的魔法棒,开启奇幻编程之旅
java·c语言·开发语言·c++·经验分享
老猿讲编程34 分钟前
一个例子来说明Ada语言的实时性支持
开发语言·ada
UestcXiye2 小时前
《TCP/IP网络编程》学习笔记 | Chapter 3:地址族与数据序列
c++·计算机网络·ip·tcp
Chrikk2 小时前
Go-性能调优实战案例
开发语言·后端·golang
幼儿园老大*2 小时前
Go的环境搭建以及GoLand安装教程
开发语言·经验分享·后端·golang·go
canyuemanyue2 小时前
go语言连续监控事件并回调处理
开发语言·后端·golang
杜杜的man2 小时前
【go从零单排】go语言中的指针
开发语言·后端·golang
霁月风3 小时前
设计模式——适配器模式
c++·适配器模式