# std::basic_filebuf< _CharT, _Traits > Class Template Reference

The actual work of input and output (for files). More...

Inheritance diagram for std::basic_filebuf< _CharT, _Traits >:

[legend]
Collaboration diagram for std::basic_filebuf< _CharT, _Traits >:

[legend]
List of all members.

## Public Types

typedef _CharT | char_type
typedef _Traits | traits_type
typedef traits_type::int_type | int_type
typedef traits_type::pos_type | pos_type
typedef traits_type::off_type | off_type

## Public Methods

| basic_filebuf ()
| Does not open any files.
virtual | ~basic_filebuf ()
| The destructor closes the file first.
bool | is_open () const
| Returns true if the external file is open.
__filebuf_type * | open (const char *__s, ios_base::openmode __mode)
| Opens an external file.
__filebuf_type * | close ()
| Closes the currently associated file.

## Protected Methods

virtual streamsize | showmanyc ()
| Investigating the data available.
virtual int_type | underflow ()
| Fetches more data from the controlled sequence.
virtual int_type | uflow ()
| Fetches more data from the controlled sequence.
virtual int_type | pbackfail (int_type __c=_Traits::eof())
| Tries to back up the input sequence.
virtual int_type | overflow (int_type __c=_Traits::eof())
| Consumes data from the buffer; writes to the controlled sequence.
virtual __streambuf_type * | setbuf (char_type *__s, streamsize __n)
| Manipulates the buffer.
virtual pos_type | seekoff (off_type __off, ios_base::seekdir __way, ios_base::openmode __mode=ios_base::in|ios_base::out)
| Alters the stream positions.
virtual pos_type | seekpos (pos_type __pos, ios_base::openmode __mode=ios_base::in|ios_base::out)
| Alters the stream positions.
virtual int | sync ()
| Synchronizes the buffer arrays with the controlled sequences.
virtual void | imbue (const locale &__loc)
| Changes translations.
virtual streamsize | xsgetn (char_type *__s, streamsize __n)
| Multiple character extraction.
virtual streamsize | xsputn (const char_type *__s, streamsize __n)
| Multiple character insertion.

## Detailed Description

### template<typename _CharT, typename _Traits>
class std::basic_filebuf< _CharT, _Traits >

The actual work of input and output (for files).

This class associates both its input and output sequence with an external disk file, and maintains a joint file position for both sequences. Many of its sematics are described in terms of similar behavior in the Standard C Library's `FILE` streams.

Definition at line 63 of file fstream.

## Member Typedef Documentation

template<typename _CharT, typename _Traits>
typedef _CharT std::basic_filebuf< _CharT, _Traits >::char_type

These are standard types. They permit a standardized way of referring to names of (or names dependant on) the template parameters, which are specific to the implementation.

Reimplemented from std::basic_streambuf< _CharT, _Traits > .

Reimplemented in __gnu_cxx::stdio_filebuf< _CharT, _Traits > .

Definition at line 67 of file fstream.

Referenced by std::basic_filebuf< _CharT, _Traits >::pbackfail() , and std::basic_filebuf< _CharT, enc_char_traits< _CharT > >::uflow() .

template<typename _CharT, typename _Traits>
typedef traits_type::int_type std::basic_filebuf< _CharT, _Traits >::int_type

These are standard types. They permit a standardized way of referring to names of (or names dependant on) the template parameters, which are specific to the implementation.

Reimplemented from std::basic_streambuf< _CharT, _Traits > .

Reimplemented in __gnu_cxx::stdio_filebuf< _CharT, _Traits > .

Definition at line 69 of file fstream.

Referenced by std::basic_filebuf< _CharT, _Traits >::close() , std::basic_filebuf< _CharT, _Traits >::overflow() , std::basic_filebuf< _CharT, _Traits >::pbackfail() , and std::basic_filebuf< _CharT, enc_char_traits< _CharT > >::uflow() .

template<typename _CharT, typename _Traits>
typedef traits_type::off_type std::basic_filebuf< _CharT, _Traits >::off_type

These are standard types. They permit a standardized way of referring to names of (or names dependant on) the template parameters, which are specific to the implementation.

Reimplemented from std::basic_streambuf< _CharT, _Traits > .

Reimplemented in __gnu_cxx::stdio_filebuf< _CharT, _Traits > .

Definition at line 71 of file fstream.

Referenced by std::basic_filebuf< _CharT, _Traits >::seekoff() , std::basic_filebuf< _CharT, _Traits >::seekpos() , and std::basic_filebuf< _CharT, enc_char_traits< _CharT > >::uflow() .

template<typename _CharT, typename _Traits>
typedef traits_type::pos_type std::basic_filebuf< _CharT, _Traits >::pos_type

These are standard types. They permit a standardized way of referring to names of (or names dependant on) the template parameters, which are specific to the implementation.

Reimplemented from std::basic_streambuf< _CharT, _Traits > .

Reimplemented in __gnu_cxx::stdio_filebuf< _CharT, _Traits > .

Definition at line 70 of file fstream.

Referenced by std::basic_filebuf< _CharT, _Traits >::seekoff() , and std::basic_filebuf< _CharT, enc_char_traits< _CharT > >::uflow() .

template<typename _CharT, typename _Traits>
typedef _Traits std::basic_filebuf< _CharT, _Traits >::traits_type

| These are standard types. They permit a standardized way of referring to names of (or names dependant on) the template parameters, which are specific to the implementation. Reimplemented from std::basic_streambuf< _CharT, _Traits > . Reimplemented in __gnu_cxx::stdio_filebuf< _CharT, _Traits > . Definition at line 68 of file fstream.

## Constructor & Destructor Documentation

template<typename _CharT, typename _Traits>
std::basic_filebuf< _CharT, _Traits >::basic_filebuf | ( |  | ) |

| Does not open any files. The default constructor initializes the parent class using its own default ctor. Definition at line 75 of file fstream.tcc.

template<typename _CharT, typename _Traits>
virtual std::basic_filebuf< _CharT, _Traits >::~basic_filebuf | ( |  | ) | `[inline, virtual]`

| The destructor closes the file first. Definition at line 157 of file fstream.

## Member Function Documentation

template<typename _CharT, typename _Traits>
basic_filebuf< _CharT, _Traits >::__filebuf_type * std::basic_filebuf< _CharT, _Traits >::close | ( |  | ) |

Closes the currently associated file.

Returns:
  `this` on success, NULL on failure

If no file is currently open, this function immediately fails.

If a "put buffer area" exists, `overflow(eof)` is called to flush all the characters. The file is then closed.

If any operations fail, this function also fails.

Definition at line 117 of file fstream.tcc.

References std::basic_filebuf< _CharT, _Traits >::int_type , and std::basic_filebuf< _CharT, _Traits >::is_open() .

Referenced by std::basic_filebuf< _CharT, _Traits >::open() .

template<typename _CharT, typename _Traits>
void std::basic_filebuf< _CharT, _Traits >::imbue | ( | const locale & | __loc | ) | `[protected, virtual]`

Changes translations.

Parameters:

  loc | A new locale.

Translations done during I/O which depend on the current locale are changed by this call. The standard adds, "Between invocations of this function a class derived from streambuf can safely cache results of calls to locale functions and to members of facets so obtained." This function simply stores the new locale for use by derived classes.

Reimplemented from std::basic_streambuf< _CharT, _Traits > .

Definition at line 469 of file fstream.tcc.

References std::basic_streambuf< _CharT, _Traits >::eback() , std::basic_streambuf< _CharT, _Traits >::gptr() , std::basic_streambuf< _CharT, _Traits >::pbase() , and std::basic_streambuf< _CharT, _Traits >::pptr() .

template<typename _CharT, typename _Traits>
bool std::basic_filebuf< _CharT, _Traits >::is_open | ( |  | ) | const`[inline]`

Returns true if the external file is open.

Definition at line 168 of file fstream.

Referenced by std::basic_filebuf< _CharT, _Traits >::close() , std::basic_filebuf< _CharT, _Traits >::open() , std::basic_filebuf< _CharT, _Traits >::seekoff() , std::basic_filebuf< _CharT, _Traits >::setbuf() , std::basic_filebuf< _CharT, _Traits >::showmanyc() , and __gnu_cxx::stdio_filebuf< _CharT, _Traits >::stdio_filebuf() .

template<typename _CharT, typename _Traits>
basic_filebuf< _CharT, _Traits >::__filebuf_type * std::basic_filebuf< _CharT, _Traits >::open | ( | const char * | __s,
|  | ios_base::openmode | __mode
| ) |

Opens an external file.

Parameters:

  s | The name of the file.
  mode | The open mode flags.

Returns:
  `this` on success, NULL on failure

If a file is already open, this function immediately fails. Otherwise it tries to open the file named s using the flags given in mode.

[Table 92 gives the relation between openmode combinations and the equivalent fopen() flags, but the table has not been copied yet.]

Definition at line 83 of file fstream.tcc.

References std::basic_filebuf< _CharT, _Traits >::close() , std::basic_filebuf< _CharT, _Traits >::is_open() , std::basic_filebuf< _CharT, _Traits >::seekoff() , and std::basic_filebuf< _CharT, _Traits >::underflow() .

template<typename _CharT, typename _Traits>
basic_filebuf< _CharT, _Traits >::int_type std::basic_filebuf< _CharT, _Traits >::overflow | ( | int_type | __c = _Traits::eof() | ) | `[protected, virtual]`

Consumes data from the buffer; writes to the controlled sequence.

Parameters:

  c | An additional character to consume.

Returns:
  eof() to indicate failure, something else (usually c, or not_eof())

Informally, this function is called when the output buffer is full (or does not exist, as buffering need not actually be done). If a buffer exists, it is "consumed", with "some effect" on the controlled sequence. (Typically, the buffer is written out to the sequence verbatim.) In either case, the character c is also written out, if c is not `eof().`

For a formal definiton of this function, see a good text such as Langer & Kreft, or [27.5.2.4.5]/3-7.

A functioning output streambuf can be created by overriding only this function (no buffer area will be used).

Note:
  Base class version does nothing, returns eof().

Reimplemented from std::basic_streambuf< _CharT, _Traits > .

Definition at line 235 of file fstream.tcc.

References std::basic_filebuf< _CharT, _Traits >::int_type .

template<typename _CharT, typename _Traits>
basic_filebuf< _CharT, _Traits >::int_type std::basic_filebuf< _CharT, _Traits >::pbackfail | ( | int_type | __i = _Traits::eof() | ) | `[protected, virtual]`

Tries to back up the input sequence.

Parameters:

  c | The character to be inserted back into the sequence.

Returns:
  eof() on failure, "some other value" on success

Postcondition:
  The constraints of `gptr(),` `eback(),` and `pptr()` are the same as for `underflow().`

Note:
  Base class version does nothing, returns eof().

Reimplemented from std::basic_streambuf< _CharT, _Traits > .

Definition at line 167 of file fstream.tcc.

References std::basic_filebuf< _CharT, _Traits >::char_type , std::basic_filebuf< _CharT, _Traits >::int_type , std::basic_filebuf< _CharT, _Traits >::seekoff() , and std::basic_filebuf< _CharT, _Traits >::underflow() .

template<typename _CharT, typename _Traits>
basic_filebuf< _CharT, _Traits >::pos_type std::basic_filebuf< _CharT, _Traits >::seekoff | ( | off_type | __off,
|  | ios_base::seekdir | __way,
|  | ios_base::openmode | __mode = ios_base::in | ios_base::out
| ) | `[protected, virtual]`

Alters the stream positions.

Each derived class provides its own appropriate behavior.

Note:
  Base class version does nothing, returns a `pos_type` that represents an invalid stream position.

Reimplemented from std::basic_streambuf< _CharT, _Traits > .

Definition at line 398 of file fstream.tcc.

References std::basic_filebuf< _CharT, _Traits >::is_open() , std::max() , std::basic_filebuf< _CharT, _Traits >::off_type , std::basic_filebuf< _CharT, _Traits >::pos_type , and std::basic_filebuf< _CharT, _Traits >::sync() .

Referenced by std::basic_filebuf< _CharT, _Traits >::open() , std::basic_filebuf< _CharT, _Traits >::pbackfail() , and std::basic_filebuf< _CharT, _Traits >::seekpos() .

template<typename _CharT, typename _Traits>
basic_filebuf< _CharT, _Traits >::pos_type std::basic_filebuf< _CharT, _Traits >::seekpos | ( | pos_type | __pos,
|  | ios_base::openmode | __mode = ios_base::in | ios_base::out
| ) | `[protected, virtual]`

Alters the stream positions.

Each derived class provides its own appropriate behavior.

Note:
  Base class version does nothing, returns a `pos_type` that represents an invalid stream position.

Reimplemented from std::basic_streambuf< _CharT, _Traits > .

Definition at line 452 of file fstream.tcc.

References std::basic_filebuf< _CharT, _Traits >::off_type , and std::basic_filebuf< _CharT, _Traits >::seekoff() .

template<typename _CharT, typename _Traits>
basic_filebuf< _CharT, _Traits >::__streambuf_type * std::basic_filebuf< _CharT, _Traits >::setbuf | ( | char_type * | __s,
|  | streamsize | __n
| ) | `[protected, virtual]`

Manipulates the buffer.

Parameters:

  s | Pointer to a buffer area.
  n | Size of s.

Returns:
  `this`

If no file has been opened, and both s and n are zero, then the stream becomes unbuffered. Otherwise, `s` is used as a buffer; see http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2 for more.

Reimplemented from std::basic_streambuf< _CharT, _Traits > .

Definition at line 373 of file fstream.tcc.

References std::basic_filebuf< _CharT, _Traits >::is_open() .

template<typename _CharT, typename _Traits>
streamsize std::basic_filebuf< _CharT, _Traits >::showmanyc | ( |  | ) | `[protected, virtual]`

Investigating the data available.

Returns:
  An estimate of the number of characters available in the input sequence, or -1.

"If it returns a positive value, then successive calls to `underflow()` will not return `traits::eof()` until at least that number of characters have been supplied. If `showmanyc()` returns -1, then calls to `underflow()` or `uflow()` will fail." [27.5.2.4.3]/1

Note:
  Base class version does nothing, returns zero.

  The standard adds that "the intention is not only that the calls [to underflow or uflow] will not return `eof()` but that they will return "immediately".

  The standard adds that "the morphemes of `showmanyc` are "es-how-many-see", not "show-manic".

Reimplemented from std::basic_streambuf< _CharT, _Traits > .

Definition at line 153 of file fstream.tcc.

References std::basic_filebuf< _CharT, _Traits >::is_open() .

template<typename _CharT, typename _Traits>
virtual int std::basic_filebuf< _CharT, _Traits >::sync | ( |  | ) | `[inline, protected, virtual]`

Synchronizes the buffer arrays with the controlled sequences.

Returns:
  -1 on failure.

Each derived class provides its own appropriate behavior, including the definition of "failure".

Note:
  Base class version does nothing, returns zero.

Reimplemented from std::basic_streambuf< _CharT, _Traits > .

Definition at line 323 of file fstream.

Referenced by std::basic_filebuf< _CharT, _Traits >::seekoff() .

template<typename _CharT, typename _Traits>
virtual int_type std::basic_filebuf< _CharT, _Traits >::uflow | ( |  | ) | `[inline, protected, virtual]`

Fetches more data from the controlled sequence.

Returns:
  The first character from the pending sequence.

Informally, this function does the same thing as `underflow(),` and in fact is required to call that function. It also returns the new character, like `underflow()` does. However, this function also moves the read position forward by one.

Reimplemented from std::basic_streambuf< _CharT, _Traits > .

Definition at line 250 of file fstream.

template<typename _CharT, typename _Traits>
virtual int_type std::basic_filebuf< _CharT, _Traits >::underflow | ( |  | ) | `[inline, protected, virtual]`

Fetches more data from the controlled sequence.

Returns:
  The first character from the pending sequence.

Informally, this function is called when the input buffer is exhausted (or does not exist, as buffering need not actually be done). If a buffer exists, it is "refilled". In either case, the next available character is returned, or `traits::eof()` to indicate a null pending sequence.

For a formal definiton of the pending sequence, see a good text such as Langer & Kreft, or [27.5.2.4.3]/7-14.

A functioning input streambuf can be created by overriding only this function (no buffer area will be used). For an example, see http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#6

Note:
  Base class version does nothing, returns eof().

Reimplemented from std::basic_streambuf< _CharT, _Traits > .

Definition at line 246 of file fstream.

Referenced by std::basic_filebuf< _CharT, _Traits >::open() , and std::basic_filebuf< _CharT, _Traits >::pbackfail() .

template<typename _CharT, typename _Traits>
virtual streamsize std::basic_filebuf< _CharT, _Traits >::xsgetn | ( | char_type * | __s,
|  | streamsize | __n
| ) | `[inline, protected, virtual]`

Multiple character extraction.

Parameters:

  s | A buffer area.
  n | Maximum number of characters to assign.

Returns:
  The number of characters assigned.

Fills s[0] through s[n-1] with characters from the input sequence, as if by `sbumpc().` Stops when either n characters have been copied, or when `traits::eof()` would be copied.

It is expected that derived classes provide a more efficient implementation by overriding this definition.

Reimplemented from std::basic_streambuf< _CharT, _Traits > .

Definition at line 349 of file fstream.

template<typename _CharT, typename _Traits>
virtual streamsize std::basic_filebuf< _CharT, _Traits >::xsputn | ( | const char_type * | __s,
|  | streamsize | __n
| ) | `[inline, protected, virtual]`

Multiple character insertion.

Parameters:

  s | A buffer area.
  n | Maximum number of characters to write.

Returns:
  The number of characters written.

Writes s[0] through s[n-1] to the output sequence, as if by `sputc().` Stops when either n characters have been copied, or when `sputc()` would return `traits::eof().`

It is expected that derived classes provide a more efficient implementation by overriding this definition.

Reimplemented from std::basic_streambuf< _CharT, _Traits > .

Definition at line 371 of file fstream.

The documentation for this class was generated from the following files:

- fstream
- fstream.cc
- fstream.tcc