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

相关推荐
Huazzi.几秒前
算法题解:斐波那契数列(C语言)
c语言·开发语言·算法
汉字萌萌哒2 分钟前
【2022 CCF 非专业级别软件能力认证第一轮(CSP-J1)入门级 C++语言试题及解析】
数据结构·c++·算法
th新港3 分钟前
CCF201909_1
数据结构·c++·算法·ccf
意如流水任东西3 分钟前
[C++]类和对象(上)
开发语言·c++
清纯世纪9 分钟前
基于深度学习的图像分类或识别系统(含全套项目+PyQt5界面)
开发语言·python·深度学习
孤寂大仙v9 分钟前
【C++】STL----stack和queue常见用法
开发语言·c++
孤华暗香13 分钟前
Python快速入门 —— 第三节:类与对象
开发语言·python
一丝晨光28 分钟前
逻辑运算符
java·c++·python·kotlin·c#·c·逻辑运算符
元气代码鼠29 分钟前
C语言程序设计(进阶)
c语言·开发语言·算法
霍霍哈嗨41 分钟前
【QT基础】创建项目&项目代码解释
开发语言·qt