次の方法で共有


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.

For a list of all members of this type, see basic_fstream Members.

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>.

注意

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...

Requirements

Header: <fstream>

Namespace: std

See Also

Reference

Thread Safety in the Standard C++ Library

iostream Programming

iostreams Conventions

Other Resources

<fstream> Members

basic_fstream Members