std::basic_istream<CharT,Traits>::readsome
來自 cppreference.com
< cpp | io | basic istream
std::streamsize readsome( char_type* s, std::streamsize count ); |
||
從輸入流中提取最多 count 個立即可用的字元。提取的字元儲存到 s 所指向的字元陣列中。
行為如 UnformattedInputFunction。在構造並檢查哨兵物件後:
- 如果 rdbuf()->in_avail() == -1,則呼叫 setstate(eofbit) 且不提取任何字元。
- 如果 rdbuf()->in_avail() == 0,則不提取任何字元。
- 如果 rdbuf()->in_avail() > 0,則提取 std::min(rdbuf()->in_avail(), count) 個字元,並將它們儲存到 s 指向的字元陣列的連續位置。
目錄 |
[編輯] 引數
s | - | 指向字元陣列的指標,用於儲存字元 |
count | - | 要讀取的最大字元數 |
[編輯] 返回值
實際提取的字元數。
[編輯] 異常
如果內部操作丟擲異常,則捕獲該異常並設定 badbit。如果 exceptions() 為 badbit
設定,則重新丟擲異常。
[編輯] 注意
此函式的行為高度依賴於實現。例如,將 readsome()
與 std::ifstream 一起使用會導致顯著的、依賴於實現的結果。一些庫實現在 std::ifstream 開啟檔案後立即用資料填充底層 filebuf
,這意味著 readsome()
總是讀取資料,甚至可能讀取整個檔案。在其他實現中,std::ifstream 僅在呼叫輸入操作時才從檔案中讀取,這意味著在開啟檔案後立即呼叫 readsome()
永遠不會提取任何字元。類似地,呼叫 std::cin.readsome() 可能會返回所有待處理、未處理的控制檯輸入,也可能始終返回零且不提取任何字元。
[編輯] 示例
執行此程式碼
#include <cassert> #include <iostream> #include <sstream> int main() { char c[10] = "*********"; // c[9] == '\0' // std::stringbuf makes its entire buffer available for unblocking read std::istringstream input("This is sample text."); auto r = input.readsome(c, 5); // reads 'This ' and stores in c[0] .. c[4] assert(r == 5); std::cout << c << '\n'; r = input.readsome(c, 9); // reads 'is sample' and stores in c[0] .. c[8] assert(r == 9); std::cout << c << '\n'; }
輸出
This **** is sample
[編輯] 參閱
提取字元塊 (公共成員函式) | |
獲取獲取區中立即可用的字元數 ( std::basic_streambuf<CharT,Traits> 的公共成員函式) |