Export (0) Print
Expand All

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. Consider using basic_streambuf::_Sgetn_s instead.


streamsize sgetn(
   char_type *_Ptr,
   streamsize _Count
);

Parameters

_Ptr

The buffer to contain the extracted characters.

_Count

The number of elements to read.

The number of elements read.

The member function returns 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.
    // 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;
}

testing

Output

3 tes
ting

Header: <streambuf>

Community Additions

ADD
Show:
© 2014 Microsoft