命名空間
變體
動作

輸入/輸出函式庫

出自 cppreference.com
< cpp

C++ 包含以下輸入/輸出程式庫:物件導向風格串流 I/O 程式庫基於 print 的函式系列(自 C++23 起),以及標準的 C 風格 I/O 函式。

目錄

[編輯] 串流 I/O

串流式輸入/輸出程式庫圍繞著抽象的輸入/輸出裝置進行組織。這些抽象裝置允許相同的程式碼處理檔案、記憶體串流或自訂轉接器裝置的輸入/輸出,這些裝置可以在運行時執行任意操作(例如壓縮)。

大多數類別皆為模板化,因此可以適配任何基本字元類型。對於最常見的基本字元類型(charwchar_t)提供了獨立的 typedef。這些類別組織成以下層次結構:

cpp/io/ios basecpp/io/basic ioscpp/io/basic istreamcpp/io/basic ifstreamcpp/io/basic istringstreamcpp/io/basic ostreamcpp/io/basic ofstreamcpp/io/basic ostringstreamcpp/io/basic fstreamcpp/io/basic stringstreamcpp/io/basic iostreamstd-io-complete-inheritance.svg

繼承圖

抽象 (Abstraction)
定義於標頭檔 <ios>
管理格式化旗標與輸入/輸出例外狀況
(類別) [編輯]
管理任意串流緩衝區
(類別模板) [編輯]
定義於標頭檔 <streambuf>
抽象化原始裝置
(類別模板) [編輯]
定義於標頭檔 <ostream>
包裝給定的抽象裝置 (std::basic_streambuf)
並提供高階輸出介面
(類別模板) [編輯]
定義於標頭檔 <istream>
包裝給定的抽象裝置 (std::basic_streambuf)
並提供高階輸入介面
(類別模板) [編輯]
包裝給定的抽象裝置 (std::basic_streambuf)
並提供高階輸入/輸出介面
(類別模板) [編輯]
檔案 I/O 實作 (File I/O implementation)
定義於標頭檔 <fstream>
實作原始檔案裝置
(類別模板) [編輯]
實作高階檔案串流輸入操作
(類別模板) [編輯]
實作高階檔案串流輸出操作
(類別模板) [編輯]
實作高階檔案串流輸入/輸出操作
(類別模板) [編輯]
字串 I/O 實作 (String I/O implementation)
定義於標頭檔 <sstream>
實作原始字串裝置
(類別模板) [編輯]
實作高階字串串流輸入操作
(類別模板) [編輯]
實作高階字串串流輸出操作
(類別模板) [編輯]
實作高階字串串流輸入/輸出操作
(類別模板) [編輯]
陣列 I/O 實作 (Array I/O implementations)
定義於標頭檔 <spanstream>
實作原始固定字元緩衝區裝置
(類別模板) [編輯]
實作固定字元緩衝區輸入操作
(類別模板) [編輯]
實作固定字元緩衝區輸出操作
(類別模板) [編輯]
實作固定字元緩衝區輸入/輸出操作
(類別模板) [編輯]
定義於標頭檔 <strstream>
(於 C++98 中棄用)(於 C++26 中移除)
實作原始字元陣列裝置
(類別) [編輯]
(於 C++98 中棄用)(於 C++26 中移除)
實作字元陣列輸入操作
(類別) [編輯]
(於 C++98 中棄用)(於 C++26 中移除)
實作字元陣列輸出操作
(類別) [編輯]
(於 C++98 中棄用)(於 C++26 中移除)
實作字元陣列輸入/輸出操作
(類別) [編輯]
同步輸出 (自 C++20 起)
定義於標頭檔 <syncstream>
同步輸出裝置包裝器
(類別模板) [編輯]
同步輸出串流包裝器
(類別模板) [編輯]

[編輯] 類型定義 (Typedefs)

std 命名空間中提供了以下針對常見字元類型的 typedef:

類型 定義
定義於標頭檔 <ios>
std::ios std::basic_ios<char>
std::wios std::basic_ios<wchar_t>
定義於標頭檔 <streambuf>
std::streambuf std::basic_streambuf<char>
std::wstreambuf std::basic_streambuf<wchar_t>
定義於標頭檔 <istream>
std::istream std::basic_istream<char>
std::wistream std::basic_istream<wchar_t>
std::iostream std::basic_iostream<char>
std::wiostream std::basic_iostream<wchar_t>
定義於標頭檔 <ostream>
std::ostream std::basic_ostream<char>
std::wostream std::basic_ostream<wchar_t>
定義於標頭檔 <fstream>
std::filebuf std::basic_filebuf<char>
std::wfilebuf std::basic_filebuf<wchar_t>
std::ifstream std::basic_ifstream<char>
std::wifstream std::basic_ifstream<wchar_t>
std::ofstream std::basic_ofstream<char>
std::wofstream std::basic_ofstream<wchar_t>
std::fstream std::basic_fstream<char>
std::wfstream std::basic_fstream<wchar_t>
定義於標頭檔 <sstream>
std::stringbuf std::basic_stringbuf<char>
std::wstringbuf std::basic_stringbuf<wchar_t>
std::istringstream std::basic_istringstream<char>
std::wistringstream std::basic_istringstream<wchar_t>
std::ostringstream std::basic_ostringstream<char>
std::wostringstream std::basic_ostringstream<wchar_t>
std::stringstream std::basic_stringstream<char>
std::wstringstream std::basic_stringstream<wchar_t>
定義於標頭檔 <spanstream>
std::spanbuf (C++23) std::basic_spanbuf<char>
std::wspanbuf (C++23) std::basic_spanbuf<wchar_t>
std::ispanstream (C++23) std::basic_ispanstream<char>
std::wispanstream (C++23) std::basic_ispanstream<wchar_t>
std::ospanstream (C++23) std::basic_ospanstream<char>
std::wospanstream (C++23) std::basic_ospanstream<wchar_t>
std::spanstream (C++23) std::basic_spanstream<char>
std::wspanstream (C++23) std::basic_spanstream<wchar_t>
定義於標頭檔 <syncstream>
std::syncbuf (C++20) std::basic_syncbuf<char>
std::wsyncbuf (C++20) std::basic_syncbuf<wchar_t>
std::osyncstream (C++20) std::basic_osyncstream<char>
std::wosyncstream (C++20) std::basic_osyncstream<wchar_t>

[編輯] 預定義標準串流物件

定義於標頭檔 <iostream>
從標準 C 輸入串流 stdin 讀取
(全域物件)[編輯]
寫入標準 C 輸出串流 stdout
(全域物件)[編輯]
寫入標準 C 錯誤串流 stderr,無緩衝
(全域物件)[編輯]
寫入標準 C 錯誤串流 stderr
(全域物件)[編輯]

[編輯] I/O 操縱子 (Manipulators)

串流式 I/O 程式庫使用 I/O 操縱子(例如 std::boolalphastd::hex 等)來控制串流的行為方式。

[編輯] 類型 (Types)

定義了以下輔助類型:

定義於標頭檔 <ios>
代表相對檔案/串流位置(從 fpos 的位移),足以代表任何檔案大小
(typedef) [編輯]
代表 I/O 操作中傳輸的字元數或 I/O 緩衝區的大小
(typedef) [編輯]
代表串流或檔案中的絕對位置
(類別模板) [編輯]

提供了以下針對 std::fpos<std::mbstate_t> 的 typedef 名稱:

定義於標頭檔 <iosfwd>
類型 定義
std::streampos std::fpos<std::char_traits<char>::state_type>
std::wstreampos std::fpos<std::char_traits<wchar_t>::state_type>
std::u8streampos (C++20) std::fpos<std::char_traits<char8_t>::state_type>
std::u16streampos (C++11) std::fpos<std::char_traits<char16_t>::state_type>
std::u32streampos (C++11) std::fpos<std::char_traits<char32_t>::state_type>

[編輯] 錯誤類別介面 (自 C++11 起)

定義於標頭檔 <ios>
(C++11)
IO 串流錯誤代碼
(列舉) [編輯]
識別 iostream 錯誤類別
(函式) [編輯]

[編輯] Print 函式 (自 C++23 起)

支援 Unicode 的 print 系列函式,對已經格式化的文字執行格式化 I/O。它們帶來了 std::format 的所有效能優勢,預設與區域設定(locale)無關,減少全域狀態,避免配置臨時的 std::string 物件並呼叫 operator<<,總體而言,與 iostreamsstdio 相比,使格式化更有效率。

提供了以下 print 類型的函式

定義於標頭檔 <print>
(C++23)
使用參數的 格式化 表示輸出到 stdout 或檔案串流
(函式模板) [編輯]
(C++23)
std::print 相同,差別在於每個 print 都會以額外的換行符結尾
(函式模板) [編輯]
使用 型別消除 (type-erased) 的參數表示輸出到支援 Unicode 的 stdout 或檔案串流
(函式) [編輯]
使用 型別消除 (type-erased) 的參數表示輸出到 stdout 或檔案串流
(函式) [編輯]
 
定義於標頭檔 <ostream>
輸出引數的 格式化 表示
(函式模板) [編輯]
輸出引數的 格式化 表示,並附加 '\n'
(函式模板) [編輯]

[編輯] C 風格 I/O

C++ 也包含 由 C 定義的輸入/輸出函式,例如 std::fopenstd::getc 等。

[編輯] 參閱

檔案系統程式庫 (自 C++17 起)
English Deutsch 日本語 中文(简体) 中文(繁體)