C++ basic_filebuf Class



Introduction

The basic_filebuf class has been derived from basic_streambuf and it associates the input or output sequence with a file. Each object of type basic_filebuf controls two character sequences:

  • A character input sequence

  • A character output sequence

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.

Class Synopsis

#include <fstream>

namespace std {
  template<class charT, class traits = char_traits<charT> >
  class basic_filebuf;
}

Class Constructors

Sr.No. Constructors & Description
1 basic_filebuf()
This constructor constructs an object of class basic_filebuf, initializing the base class with basic_streambuf<charT,traits>().
2 basic_filebuf(int fd, char_type *buf = 0, streamsize n = /* default size */);)
This constructor constructs an object of class basic_filebuf, initializing the base class with basic_streambuf<charT,traits>() and finally it calls open(fd, buf, n).
3 basic_filebuf(FILE *fp, char_type *buf = 0, streamsize n = /* default size */);)
This constructor constructs an object of class basic_filebuf, initializing the base class with basic_streambuf<charT,traits>() and finally it calls open(fp, buf, n).

Class Destructors

Sr.No. Destructors & Description
1 virtual ~basic_filebuf()
This destructor calls close() function to close the open streams and destroys the created basic_filebuf object.

Class Functions

Sr.No. Functions & Description
1 basic_filebuf<charT, traits>* attach(int fd)
This function attaches the basic_filebuf object to the open file descriptor fd.
2 int fd() const
This function returns the associated file descriptor if successful otherwise -1 is returned.
3 basic_filebuf<charT,traits>* close()
This function flushes the put area buffer and closes the associated file.
4 int detach()
This function flushes any pending output to the file associated with the object.
5 bool is_open() const
This function returns true if the associated file is open.
6 basic_filebuf<charT,traits>* open(const char* fname, ios_base::openmode mode, long protection = 0666)
This function opens a file and configures it as the associated character sequence.
7 basic_filebuf<charT,traits>* open(int fd, char_type *buf = 0, streamsize n = /* default size */)
This function attaches previously opened file descriptor fd to the basic_filebuf object.
8 basic_filebuf<charT,traits>* open(FILE *fp, char_type *buf = 0, streamsize n = /* default size */)
This function attaches previously opened file pointer fp to the basic_filebuf object.
9 int_type overflow(int_type c = traits_type::eof())
This function write the data from the put area to the associated character sequence (to the file).
10 int_type pbackfail(int_type c = traits_type::eof())
This function puts the character c back into the get area.
11 pos_type seekoff(off_type off, ios_base::seekdir way, ios_base::openmode which = ios_base::in | ios_base::out)
This function repositions the file pointer, if possible, to the position that corresponds to exactly off characters from beginning, end, or current position of the file.
12 pos_type seekpos(pos_type sp, ios_base::openmode which = ios_base::in | ios_base::out)
This function repositions the file pointer, if possible, to the position indicated by sp.
13 basic_filebuf<charT,traits>* setbuf(char_type *s, streamsize n)
This function provides user-supplied buffer or turns this filebuf unbuffered .
14 int sync()
This function synchronizes the contents of the external file, with its image maintained in memory by the file buffer.
15 int_type underflow()
This function reads from the associated file.
16 streamsize xsputn(const char_type* s, streamsize n)
This function writes up to n characters to the output sequence.
Advertisements