public member function
<streambuf> <iostream>
int_type sputc (char_type c);
Put character and advance to next position
The character c is stored at the current position of the controlled output sequence, and then advances the position indicator to the next character.
Internally, the function calls the virtual protected member overflow if there are no write positions available at the put pointer (pptr). Otherwise, the function uses the put pointer (pptr) directly, without calling virtual member functions.
其行為等同於如下實現:
1 2 3 4 5 6 7
|
int_type sputc(char_type c) {
if ( (!pptr()) || (pptr()==epptr()) )
return overflow(traits_type::to_int_type(c));
*pptr()=c;
pbump(1);
return traits_type::to_int_type(c);
}
|
引數
- c
- Character to be put.
成員型別 char_type 是流緩衝區(第一個類模板引數)中字元的型別。
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
// typewriter - sputc() example
#include <iostream> // std::cin, std::cout, std::streambuf
#include <fstream> // std::ofstream
int main () {
char ch;
std::ofstream ostr ("test.txt");
if (ostr) {
std::cout << "Writing to file. Type a dot (.) to end.\n";
std::streambuf * pbuf = ostr.rdbuf();
do {
ch = std::cin.get();
pbuf->sputc(ch);
} while (ch!='.');
ostr.close();
}
return 0;
}
|
This example code writes every character to a file until a dot character (.
) is introduced.
資料競爭
修改*流緩衝區*物件。
同時訪問同一*流緩衝區*物件可能會導致資料競爭。
異常安全
基本保證:如果丟擲異常,則流緩衝區處於有效狀態(這也適用於標準派生類)。