命名空間
變體
動作

std::basic_ofstream

出自 cppreference.com
< cpp‎ | io
 
 
 
 
定義於標頭檔 <fstream>
template<

    class CharT,
    class Traits = std::char_traits<CharT>

> class basic_ofstream : public std::basic_ostream<CharT, Traits>

類別模板 basic_ofstream 實作對檔案串流進行高階輸出操作。它將檔案基礎的串流緩衝區 (std::basic_filebuf) 與 (std::basic_ostream) 的高階介面連結起來。

std::basic_ofstream 的一個典型實作僅包含一個非繼承資料成員:一個 std::basic_filebuf<CharT, Traits> 的實例。

cpp/io/ios basecpp/io/basic ioscpp/io/basic ostreamstd-basic ofstream-inheritance.svg

繼承圖

為常見的字元型別提供了數個 typedef:

定義於標頭檔 <fstream>
類型 定義
std::ofstream std::basic_ofstream<char>
std::wofstream std::basic_ofstream<wchar_t>

目錄

[編輯] 成員型別

成員型別 定義
char_type CharT[編輯]
traits_type Traits;若 Traits::char_type 不為 CharT,則程式形式不良 (ill-formed)。[編輯]
int_type Traits::int_type[編輯]
pos_type Traits::pos_type[編輯]
off_type Traits::off_type[編輯]
native_handle_type(C++26) 實作定義型別,且符合 可平凡複製 (TriviallyCopyable)半正規 (semiregular)[編輯]

[編輯] 成員函式

建構檔案串流
(公開成員函式) [編輯]
(解構函式)
[虛擬] (隱含宣告)
解構 basic_ofstream 及其相關聯的緩衝區,並關閉檔案
(虛擬公開成員函式) [編輯]
(C++11)
移動檔案串流
(公開成員函式) [編輯]
(C++11)
交換兩個檔案串流
(公開成員函式) [編輯]
回傳底層的原始檔案裝置物件
(公開成員函式) [編輯]
回傳底層實作定義的控制代碼 (handle)
(公開成員函式) [編輯]
檔案操作
檢查串流是否具有關聯的檔案
(公開成員函式) [編輯]
開啟檔案並將其與串流關聯
(公開成員函式) [編輯]
關閉關聯的檔案
(公開成員函式) [編輯]

[編輯] 非成員函式

特化 std::swap 演算法
(函式範本) [編輯]

繼承自 std::basic_ostream

成員函式

格式化輸出
插入格式化數據
(std::basic_ostream<CharT,Traits> 的公開成員函式) [編輯]
非格式化輸出
插入字元
(std::basic_ostream<CharT,Traits> 的公開成員函式) [編輯]
插入字元區塊
(std::basic_ostream<CharT,Traits> 的公開成員函式) [編輯]
定位
回傳輸出位置指示器
(std::basic_ostream<CharT,Traits> 的公開成員函式) [編輯]
設定輸出位置指示器
(std::basic_ostream<CharT,Traits> 的公開成員函式) [編輯]
其他
與底層儲存裝置同步
(std::basic_ostream<CharT,Traits> 的公開成員函式) [編輯]

成員類別

實作串流準備進行輸出操作的基本邏輯
(std::basic_ostream<CharT,Traits> 的公開成員類別) [編輯]

繼承自 std::basic_ios

成員型別

成員型別 定義
char_type CharT
traits_type Traits
int_type Traits::int_type
pos_type Traits::pos_type
off_type Traits::off_type

成員函式

狀態函式
檢查是否未發生錯誤,即 I/O 操作可用
(std::basic_ios<CharT,Traits> 的公開成員函式) [編輯]
檢查是否已到達檔案末尾 (end-of-file)
(std::basic_ios<CharT,Traits> 的公開成員函式) [編輯]
檢查是否發生了錯誤
(std::basic_ios<CharT,Traits> 的公開成員函式) [編輯]
檢查是否發生了不可恢復的錯誤
(std::basic_ios<CharT,Traits> 的公開成員函式) [編輯]
檢查是否發生了錯誤 (等同於 fail())
(std::basic_ios<CharT,Traits> 的公開成員函式) [編輯]
檢查是否未發生錯誤 (等同於 !fail())
(std::basic_ios<CharT,Traits> 的公開成員函式) [編輯]
回傳狀態旗標
(std::basic_ios<CharT,Traits> 的公開成員函式) [編輯]
設定狀態旗標
(std::basic_ios<CharT,Traits> 的公開成員函式) [編輯]
修改狀態旗標
(std::basic_ios<CharT,Traits> 的公開成員函式) [編輯]
格式化
複製格式化資訊
(std::basic_ios<CharT,Traits> 的公開成員函式) [編輯]
管理填充字元
(std::basic_ios<CharT,Traits> 的公開成員函式) [編輯]
其他
管理例外遮罩 (exception mask)
(std::basic_ios<CharT,Traits> 的公開成員函式) [編輯]
設定區域語言 (locale)
(std::basic_ios<CharT,Traits> 的公開成員函式) [編輯]
管理關聯的串流緩衝區
(std::basic_ios<CharT,Traits> 的公開成員函式) [編輯]
管理繫結的串流
(std::basic_ios<CharT,Traits> 的公開成員函式) [編輯]
轉換為窄字元
(std::basic_ios<CharT,Traits> 的公開成員函式) [編輯]
轉換為寬字元
(std::basic_ios<CharT,Traits> 的公開成員函式) [編輯]

繼承自 std::ios_base

成員函式

格式化
管理格式旗標
(std::ios_base 的公開成員函式) [編輯]
設定特定的格式旗標
(std::ios_base 的公開成員函式) [編輯]
清除特定的格式旗標
(std::ios_base 的公開成員函式) [編輯]
管理浮點數操作的小數精確度
(std::ios_base 的公開成員函式) [編輯]
管理欄位寬度
(std::ios_base 的公開成員函式) [編輯]
在地化 (Locales)
設定區域語言 (locale)
(std::ios_base 的公開成員函式) [編輯]
回傳當前區域語言 (locale)
(std::ios_base 的公開成員函式) [編輯]
內部可擴充陣列
[靜態]
回傳一個程式範圍內唯一的整數,可安全地用作 pword()iword() 的索引
(std::ios_base 的公開靜態成員函式) [編輯]
必要時調整私有儲存區的大小,並存取位於指定索引的 long 元素
(std::ios_base 的公開成員函式) [編輯]
必要時調整私有儲存區的大小,並存取位於指定索引的 void* 元素
(std::ios_base 的公開成員函式) [編輯]
其他
註冊事件回呼函式
(std::ios_base 的公開成員函式) [編輯]
設定 C++ 與 C I/O 程式庫是否互通
(std::ios_base 的公開靜態成員函式) [編輯]
成員類別
串流例外
(std::ios_base 的公開成員類別) [編輯]
初始化標準串流物件
(std::ios_base 的公開成員類別) [編輯]

成員型別與常數

類型 說明
串流開啟模式型別

亦定義了下列常數

常數 說明[編輯]
app 每次寫入前先移至串流末尾[編輯]
binary 二進位模式 開啟[編輯]
in 為讀取而開啟[編輯]
out 為寫入而開啟[編輯]
trunc 開啟時捨棄串流內容[編輯]
ate 開啟後立即移至串流末尾[編輯]
noreplace (C++23) 以獨佔模式開啟[編輯]

(typedef) [編輯]
格式化旗標型別

亦定義了下列常數

常數 說明[編輯]
dec 對整數 I/O 使用十進位基底:參見 std::dec[編輯]
oct 對整數 I/O 使用八進位基底:參見 std::oct[編輯]
hex 對整數 I/O 使用十六進位基底:參見 std::hex[編輯]
basefield dec | oct | hex。可用於遮罩操作[編輯]
left 左對齊 (在右側添加填充字元):參見 std::left[編輯]
right 右對齊 (在左側添加填充字元):參見 std::right[編輯]
internal 內部對齊 (在內部指定點添加填充字元):參見 std::internal[編輯]
adjustfield left | right | internal。可用於遮罩操作[編輯]
scientific 使用科學記號法產生浮點型別,若與 fixed 結合則使用十六進位記號法:參見 std::scientific[編輯]
fixed 使用定點記號法產生浮點型別,若與 scientific 結合則使用十六進位記號法:參見 std::fixed[編輯]
floatfield scientific | fixed。可用於遮罩操作[編輯]
boolalpha 以字母數字格式插入及擷取 bool 型別:參見 std::boolalpha[編輯]
showbase 為整數輸出產生表示數值基底的前綴,在貨幣 I/O 中要求貨幣指示器:參見 std::showbase[編輯]
showpoint 對浮點數輸出無條件產生小數點字元:參見 std::showpoint[編輯]
showpos 對非負數輸出產生 + 字元:參見 std::showpos[編輯]
skipws 在某些輸入操作前跳過開頭的空白字元:參見 std::skipws[編輯]
unitbuf 每次輸出操作後排空 (flush) 輸出:參見 std::unitbuf[編輯]
uppercase 在特定輸出操作中將某些小寫字母替換為其對應的大寫字母:參見 std::uppercase[編輯]

(typedef) [編輯]
串流狀態型別

亦定義了下列常數

常數 說明[編輯]
goodbit 無錯誤[編輯]
badbit 不可恢復的串流錯誤[編輯]
failbit 輸入/輸出操作失敗 (格式化或擷取錯誤)[編輯]
eofbit 關聯的輸入序列已到達檔案末尾 (end-of-file)[編輯]

(typedef) [編輯]
尋找方向型別

亦定義了下列常數

常數 說明[編輯]
beg 串流的開頭[編輯]
end 串流的末尾[編輯]
cur 串流位置指示器的當前位置[編輯]

(typedef) [編輯]
指定事件型別
(enum) [編輯]
回呼函式型別
(typedef) [編輯]

[編輯] 附註

功能測試巨集 數值 標準 功能
__cpp_lib_fstream_native_handle 202306L (C++26) 原生控制代碼 (native handles) 支援

[編輯] 範例

#include <fstream>
#include <iostream>
#include <string>
 
int main()
{
    std::string filename = "Test.b";
    {
        std::ofstream ostrm(filename, std::ios::binary);
        double d = 3.14;
        ostrm.write(reinterpret_cast<char*>(&d), sizeof d); // binary output
        ostrm << 123 << "abc" << '\n';                      // text output
    }
 
    // read back
    std::ifstream istrm(filename, std::ios::binary);
    double d;
    istrm.read(reinterpret_cast<char*>(&d), sizeof d);
    int n;
    std::string s;
    istrm >> n >> s;
    std::cout << " read back: " << d << ' ' << n << ' ' << s << '\n';
}

輸出

read back: 3.14 123 abc
English Deutsch 日本語 中文(简体) 中文(繁體)