std::basic_ofstream<CharT,Traits>::open

< cpp‎ | io‎ | basic ofstream

void open( const char *filename,
           ios_base::openmode mode = ios_base::out );
(1)
void open( const std::filesystem::path::value_type *filename,
           ios_base::openmode mode = ios_base::out );
(2) (C++17 起)
void open( const std::string &filename,                                  
           ios_base::openmode mode = ios_base::out );
(3) (C++11 起)
void open( const std::filesystem::path &filename,                                  
           ios_base::openmode mode = ios_base::out );
(4) (C++17 起)

将名为 filename 的文件打开并与文件流关联。

失败时调用 setstate(failbit)

成功时调用 clear() (C++11 起)
1-2) 等效地调用 rdbuf()->open(filename, mode | ios_base::out). (该调用效果上的细节见 std::basic_filebuf::open )。仅若 std::filesystem::path::value_typechar 才提供重载 (2) (C++17 起)
3-4) 等效地调用 (1-2) ,如同以 open(filename.c_str(), mode)

参数

filename - 要打开的文件名
mode - 指定打开模式。它是位掩码类型,定义下列常量:
 
常量 解释
app 每次写入前寻位到流结尾
binary 二进制模式打开
in 为读打开
out 为写打开
trunc 在打开时舍弃流的内容
ate 打开后立即寻位到流结尾

返回值

(无)

示例

参阅

检查流是否有关联文件
(公开成员函数)
关闭关联文件
(公开成员函数)
打开文件并配置它为关联字符序列
(std::basic_filebuf<CharT,Traits> 的公开成员函数)