ifstream::ifstream

Syntax

ifstream();

ifstream( const char* szName, int nMode = ios::in, int nProt = filebuf::openprot );

ifstream( filedesc fd );

ifstream( filedesc fd, char* pch, int nLength );

Parameters

szName

The name of the file to be opened during construction.

nMode

An integer that contains mode bits defined as ios enumerators that can be combined with the bitwise-OR (|) operator:

Value Meaning

ios::in The file is opened for input (default).
ios::nocreate If the file does not already exist, the function fails.
ios::binary Opens the file in binary mode (the default is text mode).

Note that the ios::nocreate flag is necessary if you intend to test for the file's existence (the usual case).

nProt

The file protection specification; defaults to the static integer filebuf::openprot that is equivalent to filebuf::sh_compat. The possible nProt values are as follows:

Value Meaning

filebuf::sh_compat Compatibility share mode.
filebuf::sh_none Exclusive mode—no sharing.
filebuf::sh_read Read sharing allowed.
filebuf::sh_write Write sharing allowed.

The filebuf::sh_read and filebuf::sh_write modes can be combined with the logical OR (|) operator.

fd

A file descriptor as returned by a call to the run-time function _open or _sopen; filedesc is a typedef equivalent to int.

pch

Pointer to a previously allocated reserve area of length nLength. A NULL value (or nLength = 0) indicates that the stream will be unbuffered.

nLength

The length (in bytes) of the reserve area (0 = unbuffered).

Remarks

The four ifstream constructors are described as follows:

Constructor Description

ifstream() Constructs an ifstream object without opening a file.
ifstream( const char*, int, int ) Contructs an ifstream object, opening the specified file.
ifstream( filedesc ) Constructs an ifstream object that is attached to an open file.
ifstream( filedesc, char*, int ) Constructs an ifstream object that is associated with a filebuf object. The filebuf object is attached to an open file and to a specified reserve area.

All ifstream constructors construct a filebuf object. The first three use an internally allocated reserve area, but the fourth uses a user-allocated area.