Partilhar via


basic_streambuf::sgetn

Extracts up to _Count characters from the input buffer and stores them in the provided buffer _Ptr.

This method is potentially unsafe, as it relies on the caller to check that the passed values are correct.

streamsize sgetn(
   char_type *_Ptr,
   streamsize _Count
);

Parameters

  • _Ptr
    The buffer to contain the extracted characters.

  • _Count
    The number of elements to read.

Return Value

The number of elements read. See streamsize for more information.

Remarks

The member function returns xsgetn(_Ptr, _Count).

Example

// 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.
    // Note: basic_streambuf::sgetn is potentially unsafe, consider
    // using basic_streambuf::_Sgetn_s instead.
    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;
}

Input: basic_streambuf_sgetn.txt

testing

Output

3 tes
ting

Requirements

Header: <streambuf>

Namespace: std

See Also

Reference

basic_streambuf Class

iostream Programming

iostreams Conventions

Other Resources

basic_streambuf Members