get

fstream, ifstream, and ofstream

#include <fstream>fstream();explicit fstream(const char *filename,                  ios::openmode mode = ios::in | ios::out);  ifstream(); explicit ifstream(const char *filename,                   ios::openmode mode=ios::in); ofstream(); explicit ofstream(const char *filename,                   ios::openmode mode=ios::out);

The fstream( ), ifstream( ), and ofstream( ) functions 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.) For instance, this is most common way you will see a file opened:

ifstream mystream("myfile"); 
Programming Tip 

In the old-style iostream library, the fstream constructor did not contain a default for the modeparameter. That is, it did not automatically open a stream for input and output operations. Thus, when using the old iostream library to open a stream for input and output, both ios::in and ios::out must be specified explicitly. Keep this in mind if backward compatibility with the old iostream library is a concern.

If for some reason the file cannot be opened, the value of the associated stream object will be false. 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.

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