std::basic_ofstream<CharT,Traits>:: basic_ofstream
|
basic_ofstream
(
)
;
|
(1) | |
|
explicit
basic_ofstream
(
const
char
*
filename,
std::
ios_base
::
openmode
mode
|
(2) | |
|
explicit
basic_ofstream
(
const
std
::
filesystem
::
path
::
value_type
*
filename,
std::
ios_base
::
openmode
mode
|
(3) | (自 C++17 起) |
|
explicit
basic_ofstream
(
const
std::
string
&
filename,
std::
ios_base
::
openmode
mode
|
(4) | (自 C++11 起) |
|
template
<
class
FsPath
>
explicit
basic_ofstream
(
const
FsPath
&
filename,
|
(5) | (自 C++17 起) |
|
basic_ofstream
(
basic_ofstream
&&
other
)
;
|
(6) | (自 C++11 起) |
|
basic_ofstream
(
const
basic_ofstream
&
rhs
)
=
delete
;
|
(7) | (自 C++11 起) |
构造新的文件流。
open()
调用返回空指针,则设置
setstate
(
failbit
)
。
仅当
std
::
filesystem
::
path
::
value_type
不是
char
时才提供重载
(3)
。
(C++17 起)
FsPath
为
std::filesystem::path
时参与重载决议。
(C++17 起)
注意:尽管默认模式为
out
,但其效果与
std::filebuf::open
中描述的
out | trunc
效果相同。
rdbuf()
指针),然后移动构造
std::basic_filebuf
成员,最后调用
this
-
>
set_rdbuf
(
)
将新的
basic_filebuf
安装为基类中的
rdbuf()
指针。
目录 |
参数
| filename | - | 要打开的文件名 | ||||||||||||||||
| mode | - |
指定流打开模式。可使用以下常量及其按位或组合:
|
||||||||||||||||
| other | - | 用作源的其他文件流 |
示例
#include <fstream> #include <string> #include <utility> int main() { std::ofstream f0; std::ofstream f1("test.bin", std::ios::binary); std::string name = "example.txt"; std::ofstream f2(name); std::ofstream f3(std::move(f1)); }
缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的C++标准。
| 缺陷报告 | 适用范围 | 发布时行为 | 正确行为 |
|---|---|---|---|
| LWG 3430 | C++17 | std::filesystem::path 重载导致非预期转换 | 通过改为模板实现避免 |
另请参阅
|
打开文件并将其与流关联
(公开成员函数) |
|
|
打开文件并将其配置为关联的字符序列
(
std::basic_filebuf<CharT,Traits>
的公开成员函数)
|
|
替换
rdbuf
但不清除其错误状态
(受保护成员函数) |
|
|
构造对象
(
std::basic_ostream<CharT,Traits>
的公开成员函数)
|