命名空间
变体
操作

std::ios_base::openmode

来自 cppreference.cn
< cpp‎ | io‎ | ios base
 
 
 
 
typedef /* implementation defined */ openmode;
static constexpr openmode app       = /* implementation defined */;

static constexpr openmode binary    = /* implementation defined */;
static constexpr openmode in        = /* implementation defined */;
static constexpr openmode out       = /* implementation defined */;
static constexpr openmode trunc     = /* implementation defined */;

static constexpr openmode ate       = /* implementation defined */;
static constexpr openmode noreplace = /* implementation defined */;
(C++23 起)

指定可用的文件打开标志。它是一个 位掩码类型,定义了以下常量

常量 解释
app 每次写入前定位到流的末尾
binary 二进制模式打开
in 打开以供读取
out 打开以供写入
trunc 打开时丢弃流的内容
ate 打开后立即定位到流的末尾
noreplace (C++23 起) 以独占模式打开

[编辑] 示例

#include <fstream>
#include <iostream>
#include <string>
 
int main()
{
    const char* fname = "unique_name.txt";
 
    // write to a temporary stream object
    std::fstream(fname, std::ios::out | std::ios::trunc) << "Hi";
 
    std::string s;
    std::fstream(fname, std::ios::in) >> s;
    std::cout << s << '\n';
}

输出

Hi

[编辑] 参见

打开文件并将其配置为关联的字符序列
(std::basic_filebuf<CharT,Traits> 的公共成员函数) [编辑]
构造一个 basic_stringbuf 对象
(std::basic_stringbuf<CharT,Traits,Allocator> 的公共成员函数) [编辑]