public member function
<streambuf> <iostream>
Advance to next position and get character
Advances the current position of the controlled input sequence to the next character, and returns that next character.
請注意,雖然相似,但以下成員函式的行為有所不同
成員函式,逐個字元列印字串的內容 | 描述 |
sgetc() | 返回當前位置的字元。 |
sbumpc() | 返回當前位置的字元,並將當前位置推進到下一個字元。 |
snextc() | 將當前位置推進到下一個字元,並返回該下一個字元。 |
Internally, the function first calls sbumpc, and if that function returns a valid character, the function then calls sgetc. This only calls virtual members if, at some point, there are no read positions available at the get pointer (gptr).
其行為等同於如下實現:
1 2 3 4
|
int snextc() {
if ( sbumpc() == EOF ) return EOF;
else return sgetc();
}
|
返回值
The character at the next position of the controlled input sequence, as a value of type int
.
If there are no more characters to read from the controlled input sequence, the function returns the end-of-file value (EOF).
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
// show file content - snextc() example
#include <iostream> // std::cout, std::streambuf
#include <fstream> // std::ifstream
#include <cstdio> // EOF
int main () {
std::ifstream istr ("test.txt");
if (istr) {
std::streambuf * pbuf = istr.rdbuf();
do {
char ch = pbuf->sgetc();
std::cout << ch;
} while ( pbuf->snextc() != EOF );
istr.close();
}
return 0;
}
|
This example shows the content of a file on screen, using the combination of sgetc and snextc to read the input file.
資料競爭
修改*流緩衝區*物件。
同時訪問同一*流緩衝區*物件可能會導致資料競爭。
異常安全
基本保證:如果丟擲異常,則流緩衝區處於有效狀態(這也適用於標準派生類)。
無效引數會導致未定義行為。