C++ Fstream Library - rdbuf Function


Advertisements

Description

It returns a pointer to the internal filebuf object.

Declaration

Following is the declaration for fstream::rduf.

C++11

filebuf* rdbuf() const;

Return Value

It returns a pointer to the internal filebuf object.

Exceptions

Strong guarantee − if an exception is thrown, there are no changes in the stream buffer.

Data races

  • It accesses the stream object.

  • It concurrent access to the same stream object may cause data races.

Example

In below example explains about fstream rdbuf function.

#include <fstream>
#include <cstdio>

int main () {
   std::fstream src,dest;
   src.open ("test.txt");
   dest.open ("copy.txt");

   std::filebuf* inbuf  = src.rdbuf();
   std::filebuf* outbuf = dest.rdbuf();

   char c = inbuf->sbumpc();
   while (c != EOF) {
      outbuf->sputc (c);
      c = inbuf->sbumpc();
   }

   dest.close();
   src.close();

   return 0;
}
fstream.htm
Advertisements