名稱空間
變體
操作

std::ios_base::openmode

來自 cppreference.com
< 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> 的公共成員函式) [編輯]