名稱空間
變體
操作

std::basic_streambuf<CharT,Traits>::sungetc

來自 cppreference.com
< cpp‎ | io‎ | basic streambuf
 
 
 
 
int_type sungetc();

若在獲取區中有可用放回位置(gptr() > eback()),則自減下一指標(gptr())並返回它現在所指向的字元。

若無可用放回位置,則呼叫 pbackfail() 以儘可能地備份輸入序列。

I/O 流函式 basic_istream::unget 是透過此函式實現的。

目錄

[編輯] 引數

(無)

[編輯] 返回值

若放回位置可用,則返回下一指標現在所指向的字元,以 Traits::to_int_type(*gptr()) 轉換到 int_type。從此 streambuf 進行的下個單字元輸入將返回此字元。

若放回位置不可用,則返回 pbackfail() 的返回值,失敗時為 Traits::eof()

[編輯] 示例

#include <iostream>
#include <sstream>
 
int main()
{
    std::stringstream s("abcdef"); // gptr() points to 'a'
    char c1 = s.get(); // c = 'a', gptr() now points to 'b' 
    char c2 = s.rdbuf()->sungetc(); // same as s.unget(): gptr() points to 'a' again 
    char c3 = s.get(); // c3 = 'a', gptr() now points to 'b'
    char c4 = s.get(); // c4 = 'b', gptr() now points to 'c'
    std::cout << c1 << c2 << c3 << c4 << '\n';
 
    s.rdbuf()->sungetc();  // back to 'b'
    s.rdbuf()->sungetc();  // back to 'a'
    int eof = s.rdbuf()->sungetc();  // nothing to unget: pbackfail() fails
    if (eof == EOF)
            std::cout << "Nothing to unget after 'a'\n";
}

輸出

aaab
Nothing to unget after 'a'

[編輯] 參閱

將一個字元放回輸入序列
(公開成員函式) [編輯]
取消提取字元
(std::basic_istream<CharT,Traits> 的公開成員函式) [編輯]