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.
数据竞争
修改*流缓冲区*对象。
同时访问同一*流缓冲区*对象可能会导致数据竞争。
异常安全
基本保证:如果抛出异常,则流缓冲区处于有效状态(这也适用于标准派生类)。
无效参数会导致未定义行为。