命名空间
变体
操作

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 起)

指定可用的文件打开标志。它是一个 BitmaskType,定义了以下常量

常量 解释
app 每次写入前定位到流的末尾
二进制 二进制模式 打开
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> 的公共成员函数) [编辑]