Click on the banner to return to the class reference home page.

basic_fstream


basic_ostream basic_fstreambasic_iostream basic_iosios_base basic_istream

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

Synopsis

#include <fstream>  
template<class charT, class traits = char_traits<charT> > 
class basic_fstream 
: public basic_iostream<charT, traits>

Description

The template class basic_fstream<charT,traits> supports reading and writing to named files or other devices associated with a file descriptor. It uses a basic_filebuf object to control the associated sequences. It inherits from basic_iostream and can therefore use all the formatted and unformatted input and output functions.

Interface

template<class charT, class traits = char_traits<charT> > 
class basic_fstream  
: public basic_iostream<charT, traits> {

 public:

  typedef basic_ios<charT, traits>   ios_type;

  typedef charT                       char_type;
  typedef traits                      traits_type;
  typedef typename traits::int_type  int_type; 
  typedef typename traits::pos_type  pos_type; 
  typedef typename traits::off_type  off_type; 

  basic_fstream();

  explicit basic_fstream(const char *s, ios_base::openmode 
                         mode = ios_base::in | ios_base::out, 
                         long protection = 0666);

  explicit basic_fstream(int fd);

  basic_fstream(int fd, char_type *buf, int len);

  virtual ~basic_fstream();

  basic_filebuf<charT, traits> *rdbuf() const;

  bool is_open();

  void open(const char *s, ios_base::openmode mode = 
            ios_base::in | ios_base::out, 
            long protection = 0666);

  void close();

};

Types

char_type
fstream
int_type
ios_type
off_type
pos_type
traits_type
wfstream

Constructors

basic_fstream();
basic_fstream(const char* s,
              ios_base::openmode mode= 
              ios_base::in | iosw_base::out,
              long protection= 0666);
explicit basic_fstream(int fd);
basic_fstream(int fd, char_type* buf,int len);

Destructor

virtual ~basic_fstream();

Member Functions

void 
close();
bool 
is_open();
void 
open(const char* s,ios_base::openmode =
     ios_base::out | ios_base::in, 
     long protection = 0666);
basic_filebuf<charT,traits>* 
rdbuf() const;

Examples

//
// stdlib/examples/manual/fstream.cpp 
// 
#include<iostream> 
#include<bidirec>
void main ( ) 
{   
    using namespace std;

    // create a bi-directional fstream object
    fstream inout("fstream.out");

    // output characters   
    inout << "Das ist die rede von einem man" << endl;
    inout << "C'est l'histoire d'un home" << endl;
    inout << "This is the story of a man" << endl;

    char p[100];

    // seek back to the beginning of the file
    inout.seekg(0);

    // extract the first line
    inout.getline(p,100);

    // output the first line to stdout
    cout << endl << "Deutch :" << endl;
    cout << p;

    fstream::pos_type pos = inout.tellg();

    // extract the second line
    inout.getline(p,100);

    // output the second line to stdout
    cout << endl << "Francais :" << endl;
    cout << p;

    // extract the third line 
    inout.getline(p,100);

    // output the third line to stdout
    cout << endl << "English :" << endl;
    cout << p;

    // move the put sequence before the second line
    inout.seekp(pos);

    // replace the second line
    inout << "This is the story of a man" << endl;  

    // replace the third line   
    inout << "C'est l'histoire d'un home";

    // seek to the beginning of the file
    inout.seekg(0);

    // output the all content of the fstream object to stdout
    cout << endl << endl << inout.rdbuf();
}

See Also

char_traits(3C++), ios_base(3C++), basic_ios(3C++), basic_filebuf(3C++), basic_ifstream(3C++), basic_ofstream(3C++)

Working Paper for Draft Proposed International Standard for Information Systems--Programming Language C++, Section 27.8.1.11

Standards Conformance

ANSI X3J16/ISO WG21 Joint C++ Committee


©Copyright 1996, Rogue Wave Software, Inc.