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

basic_ifstream


basic_ifstreambasic_istreambasic_iosios_base

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

Synopsis

#include <fstream> 
template<class charT, class traits = char_traits<charT> >
class basic_ifstream
: public basic_istream<charT, traits>

Description

The template class basic_ifstream<charT,traits> supports reading from 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_istream and can therefore use all the formatted and unformatted input functions.

Interface

template<class charT, class traits = char_traits<charT> >
class basic_ifstream
: public basic_istream<charT, traits> {

 public:

  typedef basic_ios<charT, traits>    ios_type;

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

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

  explicit basic_ifstream(int fd);

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

  virtual ~basic_ifstream();

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

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

  void close();

};

Types

char_type
ifstream
int_type
ios_type
off_type
pos_type
traits_type
wifstream

Constructors

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

Destructor

virtual ~basic_ifstream();

Member Functions

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

Examples

//
// stdlib/examples/manual/ifstream.cpp
//
#include<iostream>
#include<fstream>
#include<iomanip>

void main ( )
{
  using namespace std;

  long   l= 20;
  char   *ntbs="Le minot passait la piece a frotter";
  char   c;
  char   buf[50];

try {

  // create a read/write file-stream object on char
  // and attach it to an ifstream object
  ifstream in("ifstream.out",ios_base::in |
              ios_base::out | ios_base::trunc);

  // tie the ostream object to the ifstream object
  ostream out(in.rdbuf());   

  // output ntbs in out
  out << ntbs << endl;

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

  // output each word on a separate line
  while ( in.get(c) )
   {
     if ( char_traits<char>::eq(c,' ') )
      cout << endl;
     else
      cout << c;
   }
  cout << endl << endl;

  // move back to the beginning of the file
  in.seekg(0); 

  // clear the state flags
  in.clear();

  // does the same thing as the previous code
  // output each word on a separate line
  while ( in >> buf )
   cout << buf << endl; 
    
  cout << endl << endl;

  // output the base info before each integer
  out << showbase;

  ostream::pos_type pos= out.tellp();

  // output l in hex with a field with of 20 
  out << hex << setw(20) << l << endl;

  // output l in oct with a field with of 20
  out << oct << setw(20) << l << endl;

  // output l in dec with a field with of 20
  out << dec << setw(20) << l << endl;

  // move back to the beginning of the file
  in.seekg(0);

  // output the all file
  cout << in.rdbuf();

  // clear the flags 
  in.clear(); 

  // seek the input sequence to pos  
  in.seekg(pos);

  int a,b,d;

  // read the previous outputted integer
  in >> a >> b >> d;

  // output 3 times 20
  cout << a << endl << b << endl << d << endl;

}
  catch( ios_base::failure& var )
   {
      cout << var.what();
   }

}

See Also

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

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

Standards Conformance

ANSI X3J16/ISO WG21 Joint C++ Committee


©Copyright 1996, Rogue Wave Software, Inc.