共用方式為


basic_streambuf::sgetn

由 _Count 字元的會從輸入緩衝區在所提供的緩衝區 _Ptr儲存它們。

這個方法是可能不安全的,因為,它便相依於該呼叫端檢查傳遞的值是正確的。

streamsize sgetn(
   char_type *_Ptr,
   streamsize _Count
);

參數

  • _Ptr
    包含擷取之字元的緩衝區。

  • _Count
    讀取的項目數目。

傳回值

讀取的項目數目。 如需詳細資訊,請參閱 streamsize

備註

成員函式傳回 xsgetn(_Ptr, _Count)。

範例

// basic_streambuf_sgetn.cpp
// compile with: /EHsc /W3
#include <iostream>
#include <fstream>

int main()
{
    using namespace std;

    ifstream myfile("basic_streambuf_sgetn.txt", ios::in);
    char a[10];

    // Extract 3 characters from myfile and store them in a.
    streamsize i = myfile.rdbuf()->sgetn(&a[0], 3);  // C4996
    a[i] = myfile.widen('\0');

    // Display the size and contents of the buffer passed to sgetn.
    cout << i << " " << a << endl;

    // Display the contents of the original input buffer.
    cout << myfile.rdbuf() << endl;
}

輸入:basic_streambuf_sgetn.txt

testing

93e7s4ye.collapse_all(zh-tw,VS.110).gifOutput

3 tes
ting

需求

標題: <streambuf>

命名空間: std

請參閱

參考

basic_streambuf Class

iostream 程式設計

iostreams 慣例