名稱空間
變體
操作

std::basic_fstream

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

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

> class basic_fstream : public std::basic_iostream<CharT, Traits>

類模板 basic_fstream 實現基於檔案的流上的高階輸入/輸出操作。它將檔案流緩衝區(std::basic_filebuf)與高階介面(std::basic_iostream)連線起來。

std::basic_fstream 的典型實現只持有一個非派生資料成員:一個 std::basic_filebuf<CharT, Traits> 例項。

cpp/io/ios basecpp/io/basic ioscpp/io/basic istreamcpp/io/basic ostreamcpp/io/basic iostreamstd-basic fstream-inheritance.svg

繼承圖

提供了用於常見字元型別的幾個 typedef

定義於標頭檔案 <fstream>
型別 定義
std::fstream std::basic_fstream<char>
std::wfstream std::basic_fstream<wchar_t>

目錄

[編輯] 成員型別

成員型別 定義
char_type CharT[編輯]
traits_type Traits;如果 Traits::char_type 不是 CharT,則程式是病態的。[編輯]
int_type Traits::int_type[編輯]
pos_type Traits::pos_type[編輯]
off_type Traits::off_type[編輯]
native_handle_type(C++26) 實現定義型別,它是TriviallyCopyablesemiregular[編輯]

[編輯] 成員函式

構造檔案流
(公共成員函式) [編輯]
(解構函式)
[virtual] (隱式宣告)
銷燬 basic_fstream 和關聯的緩衝區,關閉檔案
(虛擬公共成員函式) [編輯]
(C++11)
移動檔案流
(公共成員函式) [編輯]
(C++11)
交換兩個檔案流
(公共成員函式) [編輯]
返回底層原始檔案裝置物件
(公共成員函式) [編輯]
返回底層實現定義的控制代碼
(公共成員函式) [編輯]
檔案操作
檢查流是否關聯了檔案
(公共成員函式) [編輯]
開啟檔案並將其與流關聯
(公共成員函式) [編輯]
關閉關聯的檔案
(公共成員函式) [編輯]

[編輯] 非成員函式

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

繼承自 std::basic_istream

成員函式

格式化輸入
提取格式化資料
(std::basic_istream<CharT,Traits> 的公共成員函式) [編輯]
非格式化輸入
提取字元
(std::basic_istream<CharT,Traits> 的公共成員函式) [編輯]
讀取下一個字元而不提取它
(std::basic_istream<CharT,Traits> 的公共成員函式) [編輯]
取消提取字元
(std::basic_istream<CharT,Traits> 的公共成員函式) [編輯]
將字元放入輸入流
(std::basic_istream<CharT,Traits> 的公共成員函式) [編輯]
提取字元直到找到給定字元
(std::basic_istream<CharT,Traits> 的公共成員函式) [編輯]
提取並丟棄字元直到找到給定字元
(std::basic_istream<CharT,Traits> 的公共成員函式) [編輯]
提取字元塊
(std::basic_istream<CharT,Traits> 的公共成員函式) [編輯]
提取已有的字元塊
(std::basic_istream<CharT,Traits> 的公共成員函式) [編輯]
返回上次未格式化輸入操作提取的字元數
(std::basic_istream<CharT,Traits> 的公共成員函式) [編輯]
定位
返回輸入位置指示符
(std::basic_istream<CharT,Traits> 的公共成員函式) [編輯]
設定輸入位置指示符
(std::basic_istream<CharT,Traits> 的公共成員函式) [編輯]
雜項
與底層儲存裝置同步
(std::basic_istream<CharT,Traits> 的公共成員函式) [編輯]

成員類

為輸入操作準備流實現基本邏輯
(std::basic_istream<CharT,Traits> 的公共成員類) [編輯]

繼承自 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 特性
int_type Traits::int_type
pos_type Traits::pos_type
off_type Traits::off_type

成員函式

狀態函式
檢查是否沒有發生錯誤,即 I/O 操作可用
(std::basic_ios<CharT,Traits> 的公共成員函式) [編輯]
檢查是否已到達檔案末尾
(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> 的公共成員函式) [編輯]
雜項
管理異常掩碼
(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> 的公共成員函式) [編輯]

繼承自 std::ios_base

成員函式

格式化
管理格式標誌
(std::ios_base 的公共成員函式) [編輯]
設定特定格式標誌
(std::ios_base 的公共成員函式) [編輯]
清除特定格式標誌
(std::ios_base 的公共成員函式) [編輯]
管理浮點運算的十進位制精度
(std::ios_base 的公共成員函式) [編輯]
管理欄位寬度
(std::ios_base 的公共成員函式) [編輯]
區域設定
設定區域設定
(std::ios_base 的公共成員函式) [編輯]
返回當前區域設定
(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 每次寫入前將流定位到末尾[編輯]
二進位制 二進位制模式開啟[編輯]
in 為讀取開啟[編輯]
out 為寫入開啟[編輯]
trunc 開啟時丟棄流的內容[編輯]
ate 開啟後立即將流定位到末尾[編輯]
noreplace (C++23) 以獨佔模式開啟[編輯]

(型別定義) [編輯]
格式化標誌型別

還定義了以下常量

常量 解釋[編輯]
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 在每個輸出操作後重新整理輸出:參閱 std::unitbuf[編輯]
uppercase 在某些輸出操作中用其大寫等價物替換某些小寫字母:參閱 std::uppercase[編輯]

(型別定義) [編輯]
流狀態型別

還定義了以下常量

常量 解釋[編輯]
goodbit 無錯誤[編輯]
badbit 不可恢復的流錯誤[編輯]
failbit 輸入/輸出操作失敗(格式化或提取錯誤)[編輯]
eofbit 關聯的輸入序列已到達檔案末尾[編輯]

(型別定義) [編輯]
定位方向型別

還定義了以下常量

常量 解釋[編輯]
beg 流的開始[編輯]
end 流的結束[編輯]
cur 流位置指示器的當前位置[編輯]

(型別定義) [編輯]
指定事件型別
(列舉) [編輯]
回撥函式型別
(型別定義) [編輯]

[編輯] 注意

特性測試 標準 特性
__cpp_lib_fstream_native_handle 202306L (C++26) 原生控制代碼支援

[編輯] 示例

#include <fstream>
#include <iostream>
#include <string>
 
int main()
{
    std::string filename{"test.bin"};
    std::fstream s{filename, s.binary | s.trunc | s.in | s.out};
 
    if (!s.is_open())
        std::cout << "failed to open " << filename << '\n';
    else
    {
        // write
        double d{3.14};
        s.write(reinterpret_cast<char*>(&d), sizeof d); // binary output
        s << 123 << "abc";                              // text output
 
        // for fstream, this moves the file position pointer (both put and get)
        s.seekp(0);
 
        // read
        d = 2.71828;
        s.read(reinterpret_cast<char*>(&d), sizeof d); // binary input
        int n;
        std::string str;
        if (s >> n >> str)                             // text input
            std::cout << "read back from file: " << d << ' ' << n << ' ' << str << '\n';
    }
}

輸出

read back from file: 3.14 123 abc

[編輯] 另請參見

從 I/O 流讀取資料到字串
(函式模板) [編輯]