public member function
<streambuf> <iostream>

std::streambuf::sgetc

int sgetc();
Get current character
Returns the character at the current position of the controlled input sequence, without modifying the current position.

請注意,雖然相似,但以下成員函式的行為有所不同
成員函式,逐個字元列印字串的內容描述
sgetc()返回當前位置的字元。
sbumpc()返回當前位置的字元,並將當前位置推進到下一個字元。
snextc()將當前位置推進到下一個字元,並返回該下一個字元。

Internally, the function calls the virtual protected member underflow if there are no read positions available at the get pointer (gptr). Otherwise, the function uses the get pointer (gptr) directly, without calling virtual member functions.

其行為等同於如下實現:
1
2
3
4
int sgetc() {
  if ( (!gptr()) || (gptr()==egptr()) ) return underflow();
  else return *gptr();
}

引數



返回值

受控輸入序列當前位置的字元,型別為 int
如果受控輸入序列中沒有更多字元可供讀取,則函式返回檔案結束值(EOF)。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// show file content - sgetc() 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 a file.

資料競爭

修改*流緩衝區*物件。
同時訪問同一*流緩衝區*物件可能會導致資料競爭。

異常安全

基本保證:如果丟擲異常,則流緩衝區處於有效狀態(這也適用於標準派生類)。

另見