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

相关推荐
Billy121381 分钟前
Day12-C++20 Coroutines(上):协程原理与Promise/Awaitable机制
开发语言·c++进阶学习
01二进制代码漫游日记3 分钟前
C++基础入门速通
java·开发语言·c++
Herbert_hwt4 分钟前
第六章 Java深入理解接口、函数式接口与lambda表达式
java·开发语言·算法
tudousisi22216 分钟前
二维费用01背包
c++
2601_9561219718 分钟前
高精度问题
c++
橙橙笔记23 分钟前
C++的学习第三部分
开发语言·c++·学习
码匠许师傅9 小时前
【C++ 面试真题】聊聊 C++ 的移动语义与右值引用
java·c++·面试
2603_965148119 小时前
如何解析JSON数据?API返回的商品信息处理教程
开发语言·数据库·python·自动化·json·api
小小龙学IT10 小时前
DuckDB 深度实战:用 C++ 在进程内跑一个「分析型数据库」
数据库·c++
知无不研12 小时前
lambda表达式的使用(3)
开发语言·c++·lambda