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

相关推荐
先知后行。4 小时前
python的类
开发语言·python
派大鑫wink4 小时前
【Day12】String 类详解:不可变性、常用方法与字符串拼接优化
java·开发语言
柏木乃一4 小时前
进程(6)进程切换,Linux中的进程组织,Linux进程调度算法
linux·服务器·c++·算法·架构·操作系统
JIngJaneIL4 小时前
基于springboot + vue健康管理系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot·后端
dyxal4 小时前
Python包导入终极指南:子文件如何成功调用父目录模块
开发语言·python
Trouvaille ~4 小时前
【Linux】从磁盘到文件系统:深入理解Ext2文件系统
linux·运维·网络·c++·磁盘·文件系统·inode
我居然是兔子4 小时前
Java虚拟机(JVM)内存模型与垃圾回收全解析
java·开发语言·jvm
小许好楠5 小时前
java开发工程师-学习方式
java·开发语言·学习
superman超哥5 小时前
仓颉锁竞争优化深度解析
c语言·开发语言·c++·python·仓颉
Halo_tjn5 小时前
基于 IO 流实现文件操作的专项实验
java·开发语言