Click on the banner to return to the class reference home page.

basic_filebuf


basic_filebufbasic_streambuf

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

Synopsis

#include <fstream> 
template<class charT, class traits = char_traits<charT> >
class basic_filebuf
: public basic_streambuf<charT, traits>

Description

The template class basic_filebuf is derived from basic_streambuf. It associates the input or output sequence with a file. Each object of type basic_filebuf<charT, traits> controls two character sequences:

The restrictions on reading and writing a sequence controlled by an object of class basic_filebuf<charT,traits> are the same as for reading and writing with the Standard C library files.

If the file is not open for reading the input sequence cannot be read. If the file is not open for writing the output sequence cannot be written. A joint file position is maintained for both the input and output sequences.

A file provides byte sequences. So the basic_filebuf class treats a file as the external source (or sink) byte sequence. In order to provide the contents of a file as wide character sequences, a wide-oriented file buffer called wfilebuf converts wide character sequences to multibytes character sequences (and vice versa) according to the current locale being used in the stream buffer.

Interface

template<class charT, class traits = char_traits<charT> >
class basic_filebuf 
: public basic_streambuf<charT, traits> {
  
 public:

  typedef traits                       traits_type;
  typedef charT                        char_type;
  typedef typename traits::int_type    int_type;
  typedef typename traits::pos_type    pos_type;
  typedef typename traits::off_type    off_type;

  basic_filebuf();
  basic_filebuf(int fd);

  virtual ~basic_filebuf();

  bool is_open() const;

  basic_filebuf<charT, traits>* open(const char *s,
                                     ios_base::openmode,
                                     long protection = 0666);

  basic_filebuf<charT, traits>* open(int fd);

  basic_filebuf<charT, traits>* close();

 protected:

  virtual int      showmanyc();

  virtual int_type overflow(int_type c = traits::eof());

  virtual int_type pbackfail(int_type c = traits::eof());

  virtual int_type underflow();

  virtual basic_streambuf<charT,traits>* 
    setbuf(char_type *s,streamsize n);

 
  virtual pos_type seekoff(off_type off,
                           ios_base::seekdir way,
                           ios_base::openmode which =
                           ios_base::in | ios_base::out);

  virtual pos_type seekpos(pos_type sp,
                           ios_base::openmode which =
                           ios_base::in | ios_base::out);

  virtual int sync();

  virtual streamsize xsputn(const char_type* s, streamsize n);

};

Types

char_type
filebuf
int_type
off_type
pos_type
traits_type
wfilebuf 

Constructors

basic_filebuf();
basic_filebuf(int fd);

Destructor

virtual ~basic_filebuf();

Member Functions

basic_filebuf<charT,traits>* 
close();
bool 
is_open() const;
basic_filebuf<charT,traits>* 
open(const char* s, ios_base::openmode mode, long protection = 0666);
basic_filebuf<charT,traits>*
open(int fd);
int_type 
overflow(int_type c = traits::eof() );
int_type 
pbackfail(int_type c = traits::eof() );
pos_type 
seekoff(off_type off, ios_base::seekdir way,    ios_base::openmode which =     ios_base::in | ios_base::out);
pos_type 
seekpos(pos_type sp,ios_base::openmode      which = ios_base::in | ios_base::out);
basic_filebuf<charT,traits>* 
setbuf(char_type*s, streamsize n);
int 
sync();
int_type 
underflow();
streamsize 
xsputn(const char_type* s, streamsize n);

Examples

//
// stdlib/examples/manual/filebuf.cpp
//
#include<iostream>
#include<fstream>
void main ( )
{
  using namespace std;
  // create a read/write file-stream object on tiny char
  // and attach it to the file "filebuf.out"
  ofstream out("filebuf.out",ios_base::in |           ios_base::out);
  // tie the istream object to the ofstream object
  istream in(out.rdbuf());   
  // output to out
  out << "Il errait comme un ame en peine";
  // seek to the beginning of the file
  in.seekg(0);
  // output in to the standard output
  cout << in.rdbuf() << endl;
  // close the file "filebuf.out"
  out.close();
  // open the existing file "filebuf.out"
  // and truncate it
  out.open("filebuf.out",ios_base::in |       ios_base::out | ios_base::trunc);
  // set the buffer size
  out.rdbuf()->pubsetbuf(0,4096);
  // open the source code file
  ifstream ins("filebuf.cpp");
  //output it to filebuf.out
  out << ins.rdbuf();
  // seek to the beginning of the file
  out.seekp(0);
  // output the all file to the standard output
  cout << out.rdbuf();  
}

See Also

char_traits(3C++), ios_base(3C++), basic_ios(3C++), basic_streambuf(3C++), basic_ifstream(3C++), basic_ofstream(3C++), basic_fstream(3C++)

Working Paper for Draft Proposed International Standard for Information Systems--Programming Language C++, Section 27.8.1.1

Standards Conformance

ANSI X3J16/ISO WG21 Joint C++ Committee


©Copyright 1996, Rogue Wave Software, Inc.