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 分钟前
Java 设计模式-行为型模式(更新中)
java·开发语言·设计模式
倒霉蛋小马15 分钟前
Java新特性:record关键字
java·开发语言
浪客灿心28 分钟前
项目篇:模块设计与实现
数据库·c++
budingxiaomoli38 分钟前
Spring日志
java·开发语言
牛油果子哥q40 分钟前
【C++ STL vector】C++ STL vector 终极精讲:动态数组底层原理、两倍扩容机制、迭代器失效、增删查改、性能剖析与工程避坑指南
开发语言·c++
贩卖黄昏的熊1 小时前
flex 布局快速梳理
开发语言·javascript·css3·html5
天天进步20151 小时前
Python全栈项目--校园智能宿舍管理系统
开发语言·python
CodeStats1 小时前
从 CPU 指令到 JVM 进程:彻底讲透 Java 执行 main 方法时,类加载、主线程、栈帧入栈的完整底层逻辑
java·linux·开发语言
阿正的梦工坊2 小时前
【Rust】09-泛型、Trait 与生命周期基础
开发语言·rust·c#