C++ streambuf::sgetc() 函式



C++ 的std::streambuf::sgetc()函式用於訪問輸入序列中的下一個字元,而不將其從緩衝區中移除。此函式將字元作為int返回,如果達到輸入序列的末尾則返回EOF。

語法

以下是std::streambuf::sgetc()函式的語法。

int_type sgetc();

引數

它不接受任何引數。

返回值

它返回受控輸入序列當前位置的字元,使用成員traits_type::to_int_type將其轉換為int_type型別的值。

異常

如果丟擲異常,流緩衝區處於有效狀態。

資料競爭

它修改流緩衝區物件。

示例 1

在下面的示例中,我們將考慮sgetc()函式的基本用法。

#include <iostream>
#include <sstream>
int main() {
   std::stringstream x("Welcome");
   char b;
   std::streambuf * c = x.rdbuf();
   b = c -> sgetc();
   std::cout << "Result : " << b << std::endl;
   return 0;
}

輸出

以上程式碼的輸出如下:

Result : W

示例 2

考慮以下示例,我們將使用sgetc()函式檢查流中的第一個字元是否與特定值匹配。

#include <iostream>
#include <sstream>
int main() {
   std::stringstream x("HI");
   char y;
   std::streambuf * z = x.rdbuf();
   y = z -> sgetc();
   if (y == 'H') {
      std::cout << "First Character Is Matched." << std::endl;
   } else {
      std::cout << "First Character Is Not Matched." << std::endl;
   }
   return 0;
}

輸出

以下是以上程式碼的輸出:

First Character Is Matched.
streambuf.htm
廣告
© . All rights reserved.