basic_ifstream::rdbuf

Th e 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 ifstream stream for both input and output you need to manipulate the base buffer. The function rdbuf() returns a pointer to this buffer for manipulation.

Returns a pointer to type basic_filebuf.

Listing: Example of basic_ifstream::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;
   ifstream inOut(inFile, ios::in | ios::out);
   if(!inOut.is_open()) 
      {cout << "Could not open file"; exit(1);}

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

   return 0;
}

Result:

  The File now reads:
  CodeWarrior "Software at Work"
  Registered Trademark