std::basic_ofstream<CharT,Traits>::open
来自cppreference.com
< cpp | io | basic ofstream
void
open(
const
char*
filename,
= std::ios_base::out ); |
(1) | |
void
open(
const
std::filesystem::path::value_type*
filename,
= std::ios_base::out ); |
(2) | (C++17 起) |
void
open(
const
std::string&
filename,
= std::ios_base::out ); |
(3) | (C++11 起) |
void
open(
const
std::filesystem::path&
filename,
= std::ios_base::out ); |
(4) | (C++17 起) |
将名为 filename 的文件打开并与文件流关联。
成功时调用 clear()。失败时调用 setstate(failbit)。
1,2) 等效地调用 rdbuf()->open(filename, mode | ios_base::out)(该调用效果上的细节见
std::basic_filebuf::open)。只有在
std::filesystem::path::value_type
不是 char
的情况下才会提供重载 (2)。
(C++17
起)3,4) open(filename.c_str(),
mode) 等效地调用 (1,2)。
参数
filename | - | 要打开的文件名 | ||||||||||||||||
mode | - | 指定打开模式。它是位掩码类型
(BitmaskType)
,定义下列常量:
|
返回值
(无)
示例
本节未完成 原因:暂无示例 |
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
---|---|---|---|
LWG 22 | C++98 | 不明确在成功打开时错误状态会如何变化 | 保持不变 |
LWG 409 | C++98 | 在成功打开时错误状态保持不变 | 错误状态会清除[1] |
- ↑ 覆盖了 LWG 问题 #22 的解决方案。
参阅
检查流是否有关联文件 (公开成员函数) |
|
关闭关联文件 (公开成员函数) |
|
打开文件并配置它为关联字符序列 ( std::basic_filebuf<CharT,Traits>
的公开成员函数) |