Writing to a Text File

For file output, C++ uses analogs to cout. So to prepare for file output, let’s review some basic facts about using cout for console output:

You must include the iostream header file.

• The iostream header file defines an ostream class for handling output.

• The iostream header file declares an ostream variable, or object, called cout.

• You must account for the std namespace; for example, you can use the using directive or the std:: prefix for elements such as cout and endl.

• 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 <iostream>
#include <fstream>                  // for file I/O

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();

Notice that the close() method doesn’t require a filename. That’s because outFile has already been associated with a particular file. If you forget to close a file, the program will close it automatically if the program terminates normally.

Notice that outFile can use the same methods that cout does. Not only can it use the << operator, but it can use the various formatting methods, such as setf() and precision(). These methods affect only the object that invokes the method. For example, you can provide different values for different objects:

cout.precision(2);       // use a precision of 2 for the display
outFile.precision(4);    // use a precision of 4 for file output

The main point you should remember is that after you set up an ofstream object such as outFile, you use it in precisely the same matter as you use cout.

Let’s go back to the open() method:

outFile.open("carinfo.txt");

In this case, the file carinfo.txt does not exist before the program runs. In this circumstance, the open() method creates a brand new file by that name. When the file carinfo.txt exists, what happens if you run the program again? By default, open() first truncates the file; that is, it trims carinfo.txt to zero length, discarding the current contents. The contents are then replaced with the new output. Chapter 17 reveals how to override this default behavior.


Caution

When you open an existing file for output, by default it is truncated to a length of zero bytes, so the contents are lost.


It is possible that an attempt to open a file for output might fail. For example, a file having the requested name might already exist and have restricted access. Therefore, a careful programmer would check to see if the attempt succeeded. You’ll learn the technique for this in the next example.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.186.79