basic_fstream::rdbuf

The rdbuf() function retrieves a pointer to a filebuf type buffer.

basic_filebuf<charT, traits>* rdbuf() const;

Remarks

In order to manipulate for random access or use an fstream stream you may need to manipulate the base buffer. The function rdbuf() returns a pointer to this buffer for manipulation.

A pointer to type basic_filebuf is returned.

Listing: Example of basic_fstream::rdbuf() usage
// The ewl-test file contains originally
// CodeWarrior "Software at Work"

#include <iostream>
#include <fstream> 
#include <cstdlib>

char inFile[] = "ewl-test";

int main()
{
using namespace std;
   fstream inOut;
   inOut.open(inFile, ios::in | ios::out);

   if(!inOut.is_open()) 
      {cout << "Could not open file"; exit(1);}

   char str[] = "\n\tRegistered Trademark";
   inOut.rdbuf()->pubseekoff(0,ios::end);   
   inOut << str;   
   inOut.close();
   return 0;
}

Result:

The File now reads:

CodeWarrior "Software at Work"

Registered Trademark