Visual Studio 6.0


fstream( const char* szName, int nMode, int nProt = filebuf::openprot );

fstream( filedesc fd );

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



The name of the file to be opened during construction.


An integer that contains mode bits defined as ios enumerators that can be combined with the bitwise OR ( | ) operator. The nMode parameter must have one of the following values:

  • ios::app   The function performs a seek to the end of file. When new bytes are written to the file, they are always appended to the end, even if the position is moved with the ostream::seekp function.

  • ios::ate   The function performs a seek to the end of file. When the first new byte is written to the file, it is appended to the end, but when subsequent bytes are written, they are written to the current position.

  • ios::in   The file is opened for input. The original file (if it exists) will not be truncated.

  • ios::out   The file is opened for output.

  • ios::trunc   If the file already exists, its contents are discarded. This mode is implied if ios::out is specified, and ios::ate, ios::app, and ios:in are not specified.

  • ios::nocreate   If the file does not already exist, the function fails.

  • ios::noreplace   If the file already exists, the function fails.

  • ios::binary   Opens the file in binary mode (the default is text mode).

    Note that there is no ios::in or ios::out default mode for fstream objects. You must specify both modes if your fstream object must both read and write files.


The file protection specification; defaults to the static integer filebuf::openprot, which is equivalent to the operating system default, filebuf::sh_compat, under MS-DOS. The possible nProt values are as follows:

  • filebuf::sh_compat   Compatibility share mode (MS-DOS only).

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


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


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


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


The four fstream constructors are:

  • fstream()   Constructs an fstream object without opening a file.

  • fstream( const char*, int, int )   Contructs an fstream object, opening the specified file.

  • fstream( filedesc )   Constructs an fstream object that is attached to an open file.

  • fstream( filedesc, char*, int )   Constructs an fstream object that is associated with a filebuf object. The filebuf object is attached to an open file and to a specified reserve area.

All fstream constructors construct a filebuf object. The first three use an internally allocated reserve area, but the fourth uses a user-allocated area. The user-allocated area is not automatically released during destruction.

fstream OverviewInput Stream Classes