• You can use cout with the << operator to read a variety of data types.
File output parallels this very closely:
• You must include the fstream header file.
• The fstream header file defines an ofstream class for handling output.
• You need to declare one or more ofstream variables, or objects, which you can name as you please, as long as you respect the usual naming conventions.
• You must account for the std namespace; for example, you can use the using directive or the std:: prefix for elements such as ofstream.
• You need to associate a specific ofstream object with a specific file; one way to do so is to use the open() method.
• When you’re finished with a file, you should use the close() method to close the file.
• You can use an ofstream object with the << operator to output a variety of data types.
Note that although the iostream header file provides a predefined ostream object called cout, you have to declare your own ofstream object, choosing a name for it and associating it with a file. Here’s how you declare such objects:
ofstream outFile; // outFile an ofstream object
ofstream fout; // fout an ofstream object
Here’s how you can associate the objects with particular files:
outFile.open("fish.txt"); // outFile used to write to the fish.txt file
char filename[50];
cin >> filename; // user specifies a name
fout.open(filename); // fout used to read specified file
Note that the open() method requires a C-style string as its argument. This can be a literal string or a string stored in an array.
Here’s how you can use these objects:
double wt = 125.8;
outFile << wt; // write a number to fish.txt
char line[81] = "Objects are closer than they appear.";
fout << line << endl; // write a line of text
The important point is that after you’ve declared an ofstream object and associated it with a file, you use it exactly as you would use cout. All the operations and methods available to cout, such as <<, endl, and setf(), are also available to ofstream objects, such as outFile and fout in the preceding examples.
In short, these are the main steps for using file output:
1. Include the fstream header file.
2. Create an ofstream object.
3. Associate the ofstream object with a file.
4. Use the ofstream object in the same manner you would use cout.
The program in Listing 6.15 demonstrates this approach. It solicits information from the user, sends output to the display, and then sends the same output to a file. You can use a text editor to examine the output file.
Listing 6.15. outfile.cpp
// outfile.cpp -- writing to a file
#include
#include
int main()
{
using namespace std;
char automobile[50];
int year;
double a_price;
double d_price;
ofstream outFile; // create object for output
outFile.open("carinfo.txt"); // associate with a file
cout << "Enter the make and model of automobile: ";
cin.getline(automobile, 50);
cout << "Enter the model year: ";
cin >> year;
cout << "Enter the original asking price: ";
cin >> a_price;
d_price = 0.913 * a_price;
// display information on screen with cout
cout << fixed;
cout.precision(2);
cout.setf(ios_base::showpoint);
cout << "Make and model: " << automobile << endl;
cout << "Year: " << year << endl;
cout << "Was asking $" << a_price << endl;
cout << "Now asking $" << d_price << endl;
// now do exact same things using outFile instead of cout
outFile << fixed;
outFile.precision(2);
outFile.setf(ios_base::showpoint);
outFile << "Make and model: " << automobile << endl;
outFile << "Year: " << year << endl;
outFile << "Was asking $" << a_price << endl;
outFile << "Now asking $" << d_price << endl;
outFile.close(); // done with file
return 0;
}
Note that the final section of the program in Listing 6.15 duplicates the cout section, with cout replaced by outFile. Here is a sample run of this program:
Enter the make and model of automobile: Flitz Perky
Enter the model year: 2009
Enter the original asking price: 13500
Make and model: Flitz Perky
Year: 2009
Was asking $13500.00
Now asking $12325.50
The screen output comes from using cout. If you check the directory or folder that contains the executable program, you should find a new file called carinfo.txt. (Or it may be in some other folder, depending on how the compiler is configured.) It contains the output generated by using outFile. If you open it with a text editor, you should find the following contents:
Make and model: Flitz Perky
Year: 2009
Was asking $13500.00
Now asking $12325.50
As you can see, outFile sends precisely the same sequence of characters to the carinfo.txt file that cout sends to the display.
Program Notes
After the program in Listing 6.15 declares an ofstream object, you can use the open() method to associate the object with a particular file:
ofstream outFile; // create object for output
outFile.open("carinfo.txt"); // associate with a file
When the program is done using a file, it should close the connection:
outFile.close();