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

相关推荐
minji...7 分钟前
Linux 多线程(三)线程控制,线程终止,线程中的异常问题
linux·运维·服务器·开发语言·网络·算法
KhalilRuan8 分钟前
HybridCLR的底层原理
java·开发语言
佳木逢钺8 分钟前
[开源]玄武门之变的多变量数学建模与C++模拟系统——从历史事件到量化分析
c++·opencv·数学建模
Miki Makimura10 分钟前
C++ 聊天室项目:Linux 环境搭建与问题总结
linux·开发语言·c++
tq6J5Yg1412 分钟前
.NET 10 & C# 14 New Features 新增功能介绍-带修饰符的简单 lambda 参数
开发语言·c#·.net
jinanwuhuaguo13 分钟前
OpenClaw办公人员核心技能深度培训体系:从认知重塑到数字组织构建的全链路实战指南
java·大数据·开发语言·人工智能·openclaw
旖-旎14 分钟前
分治(交易逆序对的总数)(6)
c++·算法·leetcode·排序算法·归并排序
郝学胜-神的一滴15 分钟前
[简化版 GAMES 101] 计算机图形学 03:线性代数下
开发语言·c++·线性代数·图形渲染
Dxy123931021616 分钟前
如何使用 ECharts 绘制 K 线图
开发语言·javascript
fe7tQnVan17 分钟前
从玩具到生产:基于 ChromaDB 打造工程级 RAG 系统
开发语言·c#