basic_ifstream (3C++std) - Tru64 UNIX
Standard C++ LibraryCopyright 1996, Rogue Wave Software, Inc.
NAME
basic_ifstream,fstream
This page describes the ANSI basic_ifstream class. If you would like
information on the pre-ANSI ifstream class, use the command:
man 3C++ ifstream or man ifstream.3C++
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
The type char_type is a synonym for the template parameter charT.
ifstream
The type ifstream is an instantiation of class basic_ifstream on type
char:
typedef basic_ifstream<char> ifstream;
int_type
The type int_type is a synonym of type traits::in_type.
ios_type
The type ios_type is an instantiation of class basic_ios on type charT.
off_type
The type off_type is a synonym of type traits::off_type.
pos_type
The type pos_type is a synonym of type traits::pos_type.
traits_type
The type traits_type is a synonym for the template parameter traits.
wifstream
The type wifstream is an instantiation of class basic_ifstream on type
wchar_t:
typedef basic_ifstream<wchar_t> wifstream;
CONSTRUCTORS
basic_ifstream();
Constructs an object of class basic_ifstream<charT,traits>, initializing
the base class basic_istream with the associated file buffer, which is
initialized by calling the basic_filebuf constructor
basic_filebuf<charT,traits>(). After construction, a file can be
attached to the basic_iftream object by using the open member function.
basic_ifstream(const char* s,
ios_base::openmode mode= ios_base::in,
long protection= 0666);
Constructs an object of class
basic_ifstream<charT,traits>, initializing the base class
basic_istream with the associated file buffer, which is
initialized by calling the basic_filebuf constructor
basic_filebuf<charT,traits>(). The constructor then calls
the open function open(s,mode,protection) in order to
attach the file whose name is pointed at by s, to the
basic_iftream object. The third argument protection
provides file permissions. It does not appear in the
Standard C++ description and is provided as an extension.
It determines the file read/write/execute permissions
under UNIX. It is more limited under DOS since files are
always readable and do not have special execute
permission.
explicit basic_ifstream(int fd);
Constructs an object of class basic_ifstream<charT,traits>, initializing
the base class basic_istream with the associated file buffer, which is
initialized by calling the basic_filebuf constructor
basic_filebuf<charT,traits>(). The constructor then calls the
basic_filebuf open function open(fd) in order to attach the file
descriptor fd to the basic_iftream object. This constructor is not
described in the C++ standard, and is provided as an extension in order
to manipulate pipes, sockets or other UNIX devices, that can be accessed
through file descriptors. If the function fails, it sets
ios_base::failbit.
basic_ifstream(int fd, char_type* buf,int len);
Constructs an object of class basic_ifstream<charT,traits>, initializing
the base class basic_istream with the associated file buffer, which is
initialized by calling the basic_filebuf constructor
basic_filebuf<charT,traits>(). The constructor then calls the
basic_filebuf open function open(fd) in order to attach the file
descriptor fd to the basic_iftream object. The underlying buffer is then
replaced by calling the basic_filebuf member function setbuf with
parameters buf and len. This constructor is not described in the C++
standard, and is provided as an extension in order to manipulate pipes,
sockets or other UNIX devices, that can be accessed through file
descriptors. It also maintains compatibility with the old iostreams
library. If the function fails, it sets ios_base::failbit.
DESTRUCTOR
virtual ~basic_ifstream();
Destroys an object of class basic_ifstream.
MEMBER FUNCTIONS
void
close();
Calls the associated basic_filebuf function close() and if this function
fails, it calls the basic_ios member function setstate(failbit).
bool
is_open();
Calls the associated basic_filebuf function is_open() and return its
result.
void
open(const char* s,ios_base::openmode =
ios_base::in, long protection = 0666);
Calls the associated basic_filebuf function open(s,mode,protection).
If this function fails opening the file, it calls the basic_ios
member function setstate(failbit). The third argument protection
provides file permissions. It does not appear in the Standard C++
description and is provided as an extension. It determines the file
read/write/execute permissions under UNIX. It is more limited under
DOS since files are always readable and do not have special execute
permission.
basic_filebuf<charT,traits>*
rdbuf() const;
Returns a pointer to the basic_filebuf associated with the stream.
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++std), ios_base(3C++std), basic_ios(3C++std),
basic_filebuf(3C++std), basic_ofstream(3C++std), basic_fstream(3C++std)
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
privacy and legal statement