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 小时前
Rust 练习册 100:音乐音阶生成器
开发语言·后端·rust
D_evil__2 小时前
[C++高频精进] 并发编程:线程基础
c++
风生u3 小时前
go进阶语法
开发语言·后端·golang
666HZ6663 小时前
C语言——黑店
c语言·开发语言
Gomiko3 小时前
JavaScript基础(八):函数
开发语言·javascript·ecmascript
〝七夜5693 小时前
JVM内存结构
java·开发语言·jvm
初级炼丹师(爱说实话版)3 小时前
JAVA泛型作用域与静态方法泛型使用笔记
java·开发语言·笔记
Mr_WangAndy3 小时前
C++17 新特性_第二章 C++17 语言特性_std::any和string_view
c++·string_view·c++40周年·c++17新特性·c++新特性any
技术净胜4 小时前
MATLAB二维绘图教程:plot()函数全解析(线条样式/颜色/标记/坐标轴设置)
开发语言·matlab
Slow菜鸟4 小时前
Java开发规范(八)| 安全规范—企业级应用的“架构级底线”
java·开发语言·安全