std::basic_ofstream<CharT,Traits>::open - cppreference.com
来自cppreference.com
void open( const char* filename, std::ios_base::openmode mode = std::ios_base::out ); |
(1) | |
void open( const std::filesystem::path::value_type* filename, std::ios_base::openmode mode = std::ios_base::out ); |
(2) | (C++17 起) |
void open( const std::string& filename, std::ios_base::openmode mode = std::ios_base::out ); |
(3) | (C++11 起) |
void open( const std::filesystem::path& filename, std::ios_base::openmode mode = 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)。
参数
示例
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 22 | C++98 | 不明确在成功打开时错误状态会如何变化 | 保持不变 |
| LWG 409 | C++98 | 在成功打开时错误状态保持不变 | 错误状态会清除[1] |
- ↑ 覆盖了 LWG 问题 #22 的解决方案。