ifstream::rdbuf
filebuf* rdbuf ( ) const;
ifstream
  cplusplus.com  

Get the filebuf object associated with the stream.
  Returns the filebuf object associated with the stream.

Parameters.

none
 

Return Value.
  A pointer to the filebuf object associated with the stream.
  Notice that this pointer is never NULL, even if the buffer is not associated with any file. It is a pointer to the private filebuf object.

Example.

// read file data using associated buffer's members
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  filebuf *pbuf;
  ifstream filestr;
  long size;
  char * buffer;

  filestr.open ("test.txt");

  // get pointer to associated buffer object
  pbuf=filestr.rdbuf();

  // get file size using buffer's members
  size=pbuf->pubseekoff (0,ios::end,ios::in);
  pbuf->pubseekpos (0,ios::in);

  // allocate memory to contain file data
  buffer=new char[size];

  // get file data  
  pbuf->sgetn (buffer,size);

  filestr.close();

  // write content to stdout
  cout.write (buffer,size);

  return 0;
}

Basic template member declaration ( basic_ifstream<charT,traits> ):
basic_filebuf<charT,traits> * rdbuf () const;

See also.
  ios::rdbuf
  filebuf class, ifstream class


© The C++ Resources Network, 2000