File Output

Simple Output

Having created a stream and connected it to a file, you can now write to the file just as you would to cout -- using the stream insertion operator <<. ofstream outStream; outStream.open( "output.dat" ); int age = 42; string name = "Bob"; outStream << name << " is " << age << " years old!" << endl;

Formatted Output

Although the method is simple, the look of your output is at the mercy of the C++ default formatting parameters (field width, justification, number of decimal places, etc.)

These can be controlled in a number of different ways. Formatting commands control what your output looks like and can be used on any output stream (including cout).

setf( )

Every output stream has a function named setf( ) that can be used to change the format of your output. The most common formatting commands are shown below. The text has a table of formatting commands on page 520.
Note that once issued, these commands stay in affect until reset.
  1. outStream.setf( ios::fixed );
    causes floating point numbers to be written with a decimal point. This is analagous to printf()'s %f
  2. outStream.setf( ios::showpoint );
    forces the decimal point and trailing zeros to always be shown even if not strictly mathematically necessary
  3. outStream.setf( ios::right);
    causes the output to be right-justified in the output field. This is the default setting for the output stream.
  4. outStream.setf( ios::left);
    causes the output to be left-justified in the output field.
Other output stream functions In addition to setf( ) output streams also have the following functions available.
  1. outStream.precision( int places);
    when used in conjunction with setf( ios::fixed ), precision( ) specifies the number of places to the right of the decimal point to be displayed. A call to precision() affects all floating point I/O operations for a stream until the next call to precision()
  2. outStream.width( int size);
    specifies the (minimum) number of spaces to use to output the next item.

Manipulators

The format of your output can often be controlled by inserting "manipulators" directly into the output stream, rather than calling an output stream function. Note that not all manipulators are supported by all compilers. To use the manipulators, you must #include <iomanip>
  1. setprecision( int places ) has the same effect as the output stream precision( ) function. outStream << setprecision( 2 ) << 4.3 << endl; has the same effect as outStream.precision( 2 ); outStream << 4.3 << endl;
  2. setw( int size ) has the same effect as width() outStream << setw( 5 ) << 4.3 << endl; is the same as outStream.width( 5 ); outStream << 4.3 << endl;
  3. fixed has the same effect as setf( ios::fixed )
  4. showpoint has the same effect as setf( ios::showpoint )
The example code shows the effect of these formatting options.

Well written functions should have no "side effects". This means that when writing functions that perform output, it is good programming practice to save the current settings of the stream's formatting flags and restore them after the function's output is complete. Use code such as

int savePrecision = outStream.precision( ); and int saveFlags = outStream.flags( ); to read the current file settings and then reset them with code such as outStream.precision( savePrecision ); and outStream.flags( saveFlags ): Page 523 of the text has a more complete example of this technique.


Last Modified: Monday, 28-Aug-2006 10:15:54 EDT