std::basic_ifstream
來自 cppreference.com
定義於標頭檔案 <fstream> |
||
template< class CharT, |
||
類模板 basic_ifstream
實現基於檔案的流上的高階輸入操作。它將基於檔案的流緩衝區 (std::basic_filebuf) 與高階介面 (std::basic_istream) 連線起來。
std::basic_ifstream
的典型實現只包含一個非派生資料成員:一個 std::basic_filebuf<CharT, Traits> 例項。
繼承圖
提供了用於常見字元型別的幾個 typedef
定義於標頭檔案
<fstream> | |
型別 | 定義 |
std::ifstream
|
std::basic_ifstream<char> |
std::wifstream
|
std::basic_ifstream<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) |
實現定義型別,它是可平凡複製且半正則 的 |
[編輯] 成員函式
構造檔案流 (public member function) | |
(解構函式) [virtual] (隱式宣告) |
銷燬 basic_ifstream 和關聯的緩衝區,關閉檔案(virtual public member function) |
(C++11) |
移動檔案流 (public member function) |
(C++11) |
交換兩個檔案流 (public member function) |
返回底層原始檔案裝置物件 (public member function) | |
(C++26) |
返回底層實現定義的控制代碼 (public member function) |
檔案操作 | |
檢查流是否關聯了檔案 (public member function) | |
開啟檔案並將其與流關聯 (public member function) | |
關閉關聯的檔案 (public member function) |
[編輯] 非成員函式
特化 std::swap 演算法 (function template) |
繼承自 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_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 的公共成員類) |
成員型別和常量 | |||||||||||||||||||||||||||||||||||||||
型別 | 解釋 | ||||||||||||||||||||||||||||||||||||||
流開啟模式型別 還定義了以下常量
(typedef) | |||||||||||||||||||||||||||||||||||||||
格式化標誌型別 還定義了以下常量
(typedef) | |||||||||||||||||||||||||||||||||||||||
流狀態型別 還定義了以下常量
(typedef) | |||||||||||||||||||||||||||||||||||||||
定位方向型別 還定義了以下常量
(typedef) | |||||||||||||||||||||||||||||||||||||||
指定事件型別 (列舉) | |||||||||||||||||||||||||||||||||||||||
回撥函式型別 (typedef) |
[編輯] 註釋
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_fstream_native_handle |
202306L |
(C++26) | 原生控制代碼支援 |
[編輯] 示例
執行此程式碼
#include <fstream> #include <iostream> #include <string> int main() { std::string filename = "Test.b"; // prepare a file to read double d = 3.14; std::ofstream(filename, std::ios::binary) .write(reinterpret_cast<char*>(&d), sizeof d) << 123 << "abc"; // open file for reading std::ifstream istrm(filename, std::ios::binary); if (!istrm.is_open()) std::cout << "failed to open " << filename << '\n'; else { double d; istrm.read(reinterpret_cast<char*>(&d), sizeof d); // binary input int n; std::string s; if (istrm >> n >> s) // text input std::cout << "read back from file: " << d << ' ' << n << ' ' << s << '\n'; } }
輸出
read back from file: 3.14 123 abc