名稱空間
變體
操作

std::basic_ostream<CharT,Traits>::write

來自 cppreference.com
< cpp‎ | io‎ | basic_ostream
 
 
 
 
basic_ostream& write( const char_type* s, std::streamsize count );

行為如同一個UnformattedOutputFunction。在構造並檢查 sentry 物件後,從字元陣列(其首元素由 s 指向)的連續位置輸出字元。字元被插入到輸出序列中,直到以下情況之一發生:

  • 正好插入 count 個字元
  • 插入到輸出序列失敗(在這種情況下,呼叫 setstate(badbit))。

目錄

[編輯] 引數

s - 指向要寫入的字串的指標
count - 要寫入的字元數

[編輯] 返回值

*this

[編輯] 異常

如果發生錯誤(錯誤狀態標誌不是 goodbit)且 exceptions() 已設定為在該狀態下丟擲,則丟擲 failure

如果內部操作丟擲異常,則捕獲該異常並設定 badbit。如果 exceptions()badbit 設定,則重新丟擲該異常。

[編輯] 注意

與格式化輸出函式 operator<< 不同,此函式沒有為型別 signed charunsigned char 過載。

此外,與格式化輸出函式不同,此函式在失敗時不會設定 failbit

當使用非轉換區域設定(預設區域設定為非轉換)時,std::basic_ofstream 中此函式的過載可能針對零複製批次 I/O 進行最佳化(透過過載 std::streambuf::xsputn)。

[編輯] 示例

此函式可用於輸出物件表示,即二進位制輸出

#include <iostream>
 
int main()
{
    int n = 0x41424344;
    std::cout.write(reinterpret_cast<char*>(&n), sizeof n) << '\n';
 
    char c[] = "This is sample text.";
    std::cout.write(c, 4).write("!\n", 2);
}

可能的輸出

DCBA
This!

[編輯] 參閱

插入字元資料或插入到右值流中
(函式模板) [編輯]
插入一個字元
(公共成員函式) [編輯]