Namespaces
Variants

std::ios_base:: openmode

From cppreference.net
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";
    // 写入临时流对象
    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> 的公开成员函数)