JavaScript is required to for searching.
Skip Navigation Links
Exit Print View
Oracle Solaris Studio 12.3: C++ User's Guide     Oracle Solaris Studio 12.3 Information Library
search filter icon
search icon

Document Information


Part I C++ Compiler

1.  The C++ Compiler

2.  Using the C++ Compiler

3.  Using the C++ Compiler Options

Part II Writing C++ Programs

4.  Language Extensions

5.  Program Organization

6.  Creating and Using Templates

7.  Compiling Templates

8.  Exception Handling

9.  Improving Program Performance

10.  Building Multithreaded Programs

Part III Libraries

11.  Using Libraries

12.  Using the C++ Standard Library

13.  Using the Classic iostream Library

13.1 Predefined iostreams

13.2 Basic Structure of iostream Interaction

13.3 Using the Classic iostream Library

13.3.1 Output Using iostream Defining Your Own Insertion Operator Handling Output Errors Flushing Binary Output

13.3.2 Input Using iostream

13.3.3 Defining Your Own Extraction Operators

13.3.4 Using the char* Extractor

13.3.5 Reading Any Single Character

13.3.6 Binary Input

13.3.7 Peeking at Input

13.3.8 Extracting Whitespace

13.3.9 Handling Input Errors

13.3.10 Using iostreams With stdio

13.4 Creating iostreams

13.4.1 Dealing With Files Using Class fstream Open Mode Declaring an fstream Without Specifying a File Opening and Closing Files Opening a File Using a File Descriptor Repositioning Within a File

13.5 Assignment of iostreams

13.6 Format Control

13.7 Manipulators

13.7.1 Using Plain Manipulators

13.7.2 Parameterized Manipulators

13.8 strstream: iostreams for Arrays

13.9 stdiobuf: iostreams for stdio Files

13.10 Working Withstreambuf Streams

13.10.1 streambuf Pointer Types

13.10.2 Using streambuf Objects

13.11 iostream Man Pages

13.12 iostream Terminology

14.  Building Libraries

Part IV Appendixes

A.  C++ Compiler Options

B.  Pragmas



13.4 Creating iostreams

To read or write a stream other than the predefined iostreams, you need to create your own iostream. In general, that means creating objects of types defined in the iostream library. This section discusses the various types available.

13.4.1 Dealing With Files Using Class fstream

Dealing with files is similar to dealing with standard input and standard output; classes ifstream, ofstream, and fstream are derived from classes istream, ostream, and iostream, respectively. As derived classes, they inherit the insertion and extraction operations (along with the other member functions) and also have members and constructors for use with files.

Include the file fstream.h to use any of the fstreams. Use an ifstream when you only want to perform input, an ofstream for output only, and an fstream for a stream on which you want to perform both input and output. Use the name of the file as the constructor argument.

For example, copy the file thisFile to the file thatFile as in the following example:

ifstream fromFile("thisFile");
if     (!fromFile)
    error("unable to open ’thisFile’ for input");
ofstream toFile ("thatFile");
if     (!toFile)
    error("unable to open ’thatFile’ for output");
char c;
while (toFile && fromFile.get(c)) toFile.put(c);

This code does the following: Open Mode

The mode is constructed by or-ing together bits from the enumerated type open_mode, which is a public type of class ios and has the following definition:

enum open_mode {binary=0, in=1, out=2, ate=4, app=8, trunc=0x10,
     nocreate=0x20, noreplace=0x40};

Note - The binary flag is not needed on UNIX but is provided for compatibility with systems that do need it. Portable code should use the binary flag when opening binary files.

You can open a file for both input and output. For example, the following code opens file someName for both input and output, attaching it to the fstream variable inoutFile.

fstream inoutFile("someName", ios::in|ios::out); Declaring an fstream Without Specifying a File

You can declare an fstream without specifying a file and open the file later. The following example creates the ofstream toFile for writing.

ofstream toFile;[1], ios::out); Opening and Closing Files

You can close the fstream and then open it with another file. For example, to process a list of files provided on the command line:

ifstream infile;
for (char** f = &argv[1]; *f; ++f) {*f, ios::in);
} Opening a File Using a File Descriptor

If you know a file descriptor, such as the integer 1 for standard output, you can open it as follows:

ofstream outfile;

When you open a file by providing its name to one of the fstream constructors or by using the open function, the file is automatically closed when the fstream is destroyed by a delete or when it goes out of scope. When you attach a file to an fstream, it is not automatically closed. Repositioning Within a File

You can alter the reading and writing position in a file. Several tools are supplied for this purpose.