Apache C++ Standard Library Reference Guide

## istreambuf_iterator

Library: Iterators

    istreambuf_iterator  input_iterator

- Local Index
- Summary
- Synopsis
- Description
- Interface
- Member Types
- Member Class proxy
- Constructors
- Member Operators
- Public Member Functions
- Nonmember Functions
- Example
- See Also
- Standards Conformance

### Local Index

#### Members

char_type equal() int_type | istreambuf_iterator() istream_type operator*() | operator++() streambuf_type traits_type |

#### Non-Members

operator!=() | operator==() |

### Summary

Iterator that reads successive characters from the stream buffer for which it was constructed

### Synopsis

    #include <streambuf>

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

### Description

The class template istreambuf_iterator reads successive characters from the stream buffer for which it was constructed. `operator*()` gives access to the current input character, if any, and `operator++()` advances to the next input character. If the end of stream is reached, the iterator becomes equal to the end of stream iterator value, which is constructed by the default constructor, `istreambuf_iterator()`. An istreambuf_iterator object can be used only for one-pass-algorithms.

### Interface

    namespace std {

      template<class charT, class traits = char_traits<charT> >
      class istreambuf_iterator
        : public iterator<input_iterator_tag, charT, typename
                          traits::off_type, charT*, charT&>
      {
       public:

          typedef charT                          char_type;
          typedef typename traits::int_type      int_type;
          typedef traits                         traits_type;
          typedef basic_streambuf<charT, traits> streambuf_type;
          typedef basic_istream<charT, traits>   istream_type;

          class proxy;

          istreambuf_iterator() throw();
          istreambuf_iterator(istream_type& s)  throw();
          istreambuf_iterator(streambuf_type *s) throw();
          istreambuf_iterator(const proxy& p) throw();

          char_type operator*() const;
          istreambuf_iterator& operator++();
          proxy operator++(int);
          bool equal(istreambuf_iterator& b) const;

      };

      template<class charT, class traits>
      bool operator==(const istreambuf_iterator<charT, traits>& a,
                      const istreambuf_iterator<charT, traits>& b);

      template<class charT, class traits>
      bool operator!=(const istreambuf_iterator<charT, traits>& a,
                      const istreambuf_iterator<charT, traits>& b);
    }

### Member Types

    char_type

The type `char_type` is a synonym for the template parameter `charT`.

    int_type

The type `int_type` is a synonym of type `traits_type::int_type`.

    istream_type

The type `istream_type` is a synonym of type `basic_istream<char_type, traits_type>`.

`typedef basic_istream<charT, traits> istream_type;`

    streambuf_type

The type `streambuf_type` is a synonym of type `basic_streambuf<char_type, traits_type>`.

    traits_type

The type `traits_type` is a synonym for the template parameter `traits`.

### Member Class proxy

Class istreambuf_iterator<charT,traits>::proxy is a temporary placeholder for the return value of the post-increment operator. It keeps the character pointed to by the previous value of the iterator for some possible future access.

### Constructors

    istreambuf_iterator() throw();

Constructs the end of stream iterator.

    istreambuf_iterator(istream_type& s) throw();

Constructs an istreambuf_iterator that inputs characters using the basic_streambuf object pointed to by `s.rdbuf()`. If `s.rdbuf()` is a null pointer, the istreambuf_iterator is the end-of-stream iterator.

    istreambuf_iterator(streambuf_type *s) throw();

Constructs an istreambuf_iterator that inputs characters using the basic_streambuf object pointed to by `s`. If `s` is a null pointer, the istreambuf_iterator is the end-of-stream iterator.

    istreambuf_iterator(const proxy& p) throw();

Constructs an istreambuf_iterator that uses the basic_streambuf object embedded in the proxy object.

### Member Operators

    char_type
    operator*() const;

Returns the character pointed to by the input sequence of the attached stream buffer. If no character is available, the iterator becomes equal to the end-of-stream iterator.

    istreambuf_iterator&
    operator++();

Increments the input sequence of the attached stream buffer to point to the next character. If the current character is the last one, the iterator becomes equal to the end-of-stream iterator.

    proxy
    operator++(int);

Increments the input sequence of the attached stream buffer to point to the next character. If the current character is the last one, the iterator becomes equal to the end-of-stream iterator. The proxy object returned contains the character pointed to before carrying out the post-increment operator.

### Public Member Functions

    bool
    equal(istreambuf_iterator& b) const;

Returns `true` if and only if both iterators are at end of stream, or neither is at end of stream, regardless of what stream buffer object they are using.

### Nonmember Functions

    template<class charT, class traits>
    bool
    operator==(const istreambuf_iterator<charT, traits>& a,
               const istreambuf_iterator<charT, traits>& b);

Returns `a.equal(b)`.

    template<class charT, class traits>
    bool
    operator!=(const istreambuf_iterator<charT, traits>& a,
               const istreambuf_iterator<charT, traits>& b);

Returns `!(a.equal(b))`.

### Example

    //
    //  istreambuf_iterator.cpp
    //

    #include <iostream>   // for cout, endl
    #include <fstream>    // for ofstream, istreambuf_iterator

    #include <stdio.h>    // for tmpnam () and remove ()

    int main ( )
    {
        // create a temporary filename
        const char *fname = tmpnam (0);

        if (!fname)
            return 1;

        // open the file is_iter.out for reading and writing
        std::ofstream out (fname, std::ios::out | std::ios::in |
                                  std::ios::trunc);

        // output the example sentence into the file
        out << "Ceci est un simple exemple pour démontrer le\n"
               "fonctionnement de istreambuf_iterator.";

        // seek to the beginning of the file
        out.seekp (0);

        // construct an istreambuf_iterator pointing to
        // the ofstream object underlying streambuffer
        std::istreambuf_iterator<char, std::char_traits<char> >
                                 iter (out.rdbuf ());

        // construct an end of stream iterator
        const std::istreambuf_iterator<char,
                                       std::char_traits<char> > end;

        std::cout << std::endl;

        // output the content of the file
        while (!iter.equal (end)) {

            // use both operator++ and operator*
            std::cout << *iter++;
        }

        std::cout << std::endl;

        // remove temporary file
        remove (fname);

        return 0;
    }

    Program Output:

    Ceci est un simple exemple pour démontrer le
    fonctionnement de istreambuf_iterator.

### See Also

basic_streambuf, basic_istream, ostreambuf_iterator

### Standards Conformance

ISO/IEC 14882:1998 -- International Standard for Information Systems --Programming Language C++, Section 24.5.3