Notes on using output files
Writing to an output file is very similar to writing to the monitor. You can use many of the same operators and functions, but you need to apply them to an output file object instead of the cout object.
In order to write output to the monitor, you must include the header file iostream in your program.
#include <iostream>
This header file includes a declaration for the cout object as well as prototypes for functions that are used to do output to the monitor.
To write output to a file, you must include the header file fstream.h in your program.
#include <fstream>
This header file includes prototypes for functions that perform file input and output. However, you must declare your own file object for each file that your program uses. The following statement declares an output file object (of type ofstream) called outfile.
ofstream outfile;
Before you use a file, it must be opened. This operation is performed by the open() function. As shown in the following example, the open() function takes 2 arguments.
outfile.open( "timesheet.txt", ios::out );
After you open a file in your program, you should check that the file was successfully opened. Some possible problems when opening an output file include: a) drive not ready, b) a directory (folder) in the path does not exist, and c) error reading from drive. The following code can be used to check if the open statement shown above was successful.
if (!outfile)
{
cout << "Error opening output file - program terminated" << endl;
exit(1);
}
You can use the file object name as if it were a boolean value. A value of true indicates that the file was opened successfully. A value of false indicates that the file is in an error state. If an output file goes into the error state, C++ will not automatically abort the program and no error message is automatically printed. Your program will continue to execute, but attempts to print to the file will be ignored. That is why you need to write code to check for the error, print your own error message, and terminate the program if appropriate.
After an output file has been successfully opened, you can use the insertion operator to write to the file. You can also use the same io manipulators that you use with the cout object, such as endl, setw(), setiosflags(), and setprecision().
outfile << "Timesheet Report\n\n";
When your program terminates, your output file will be closed automatically.
Note: If you want to use your output file in multiple functions, there are two ways to accomplish this:
Here is an example program that takes input from the keyboard, formats it and writes it to an output file.
// Programmer: Bob Comer
// Completed : 2/11/99
// Status : Complete
//
// Description:
// This program writes a Timesheet Report to an output file.
//
// Algorithm:
// Create an output file
// Repeat for employee numbers 1 thru 20
// Get the employee's hours worked and pay rate
// Print hours worked and pay rate to the file
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
float hoursWorked; // Number of hours worked in a week
float payRate; // Pay for one hour in dollars
int empNo; // Employee number
ofstream outfile; // File for timesheet report
// Open output file and set up floating point format
outfile.open("timesheet.txt", ios::out);
if (!outfile)
{
cout << "Error opening output file. Program terminated."
<< endl;
exit(1);
}
outfile << setiosflags(ios::fixed|ios::showpoint) << setprecision(2);
// Print report heading
outfile << "Timesheet Report\n\n";
outfile << "Employee Hours Worked Pay Rate\n";
// Repeat for employee numbers 1 thru 20
for (empNo = 1; empNo <= 20; empNo++)
{
// Get the employee's hours worked and pay rate
cout << "Enter the number of hours worked for employee "
<< empNo << ": ";
cin >> hoursWorked;
cout << "Enter the pay rate for employee "
<< empNo << ": ";
cin >> payRate;
// Print employee pay information
outfile << setw(8) << empNo
<< setw(14) << hoursWorked
<< setw(11) << payRate
<< '\n';
}
// Signal normal exit
return 0;
}