Partager via


basic_fstream Class

Describes an object that controls insertion and extraction of elements and encoded objects using a stream buffer of class basic_filebuf<Elem, Tr>, with elements of type Elem, whose character traits are determined by the class Tr.

template <class Elem, class Tr = char_traits<Elem> >
    class basic_fstream : public basic_iostream<Elem, Tr>

Parameters

  • Elem
    The basic element of the file buffer.

  • Tr
    The traits of the basic element of the file buffer (usually char_traits<Elem>).

Remarks

The object stores an object of class basic_filebuf<Elem, Tr>.

Note

The get pointer and put pointer of an fstream object are NOT independent of each other. If the get pointer moves, so does the put pointer.

Example

The following example demonstrates how to create a basic_fstream object that can be read from and written to.

// basic_fstream_class.cpp
// compile with: /EHsc

#include <fstream>
#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
    fstream fs("fstream.txt", ios::in | ios::out | ios::trunc);
    if (!fs.bad())
    {
        // Write to the file.
        fs << "Writing to a basic_fstream object..." << endl;
        fs.close();

        // Dump the contents of the file to cout.
        fs.open("fstream.txt", ios::in);
        cout << fs.rdbuf();
        fs.close();
    }
}
Writing to a basic_fstream object...

Constructors

basic_fstream

Constructs an object of type basic_fstream.

Member Functions

close

Closes a file.

is_open

Determines if a file is open.

open

Opens a file.

rdbuf

Returns the address of the stored stream buffer, of type pointer to basic_filebuf<Elem, Tr>.

swap

Exchanges the content of this object with the content of another basic_fstream object.

Requirements

Header: <fstream>

Namespace: std

See Also

Reference

Thread Safety in the C++ Standard Library

iostream Programming

iostreams Conventions