gcount

fstream, ifstream, and ofstream

#include <fstream.h>fstream();fstream(const char *filename, int mode,          int access=filebuf::openprot); fstream(int fd); fstream(int fd, char *buf, int size); ifstream(); ifstream(const char *filename,          int mode=ios::in,           int access=filebuf::openprot); ifstream(int fd); ifstream(int fd, char *buf, int size); ofstream(); ofstream(const char *filename,           int mode=ios::out,          int access=filebuf::openprot); ofstream(int fd); ofstream(int fd, char *buf, int size); 

The fstream( ), ifstream( ), and ofstream( ) are the constructors of the fstream, ifstream, and ofstream classes, respectively.

The versions of fstream( ), ifstream( ), and ofstream( ) that take no parameters create a stream that is not associated with any file. This stream can then be linked to a file using open( ).

The versions of fstream( ), ifstream( ), and ofstream( ) that take a filename for their first parameters are the most commonly used in application programs. Although it is entirely proper to open a file using the open( ) function, most of the time you will not do so because these ifstream, ofstream, and fstream constructors automatically open the file when the stream is created. The constructors have the same parameters and defaults as the open( ) function. (See open for details.) Therefore, the most common way you will see a file opened is shown in this example:

ifstream mystream("myfile");

If for some reason the file cannot be opened, the value of the associated stream variable will be zero. Therefore, whether you use a constructor to open the file or an explicit call to open( ), you will want to confirm that the file has actually been opened by testing the value of the stream.

The versions of fstream( ), ifstream( ), and ofstream( ) that take only one parameter, an already valid file descriptor, create a stream and then associate that stream with the file descriptor specified in fd.

The versions of fstream( ), ifstream( ), and ofstream( ) that take a file descriptor, a pointer to a buffer, and a size create a stream and associate it with the file descriptor specified in fd. buf must be a pointer to memory that will serve as a buffer, and size specifies the length of the buffer, in bytes. (If buf is null and/or if size is 0, no buffering takes place.)

Related functions are close( ) and open( ).




C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net