basic_streambuf::sgetn
Visual Studio 2008
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 );
The number of elements read. See streamsize for more information.
// 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;
}