
lafore_robert_objectoriented_programming_in_c
.pdf

626 |
Chapter 12 |
|
int main(int argc, char* argv[] )
{
if(argc != 2)
{
cerr << “\nFormat: oprint filename”;
exit(-1); |
|
} |
|
char ch; |
//character to read |
ifstream infile; |
//create file for input |
infile.open( argv[1] ); |
//open file |
if( !infile ) |
//check for errors |
{ |
|
cerr << “\nCan’t open “ << argv[1]; |
|
exit(-1); |
|
} |
|
ofstream outfile; |
//make file |
outfile.open(“PRN”); |
//open it for printer |
while( infile.get(ch) != 0 ) |
//read a character |
outfile.put(ch); |
//write character to printer |
outfile.put(‘\x0C’); |
//formfeed to eject page |
return 0; |
|
} |
|
You can use this program to print any text file, such as any of your .CPP source files. It acts much the same as the DOS PRINT command. Like the OTYPE example, this program checks for the correct number of command-line arguments, and for a successful opening of the specified file.
Summary
In this chapter we briefly examined the hierarchy of stream classes and showed how to handle various kinds of I/O errors. Then we saw how to perform file I/O in a variety of ways. Files in C++ are associated with objects of various classes, typically ofstream for output, ifstream for input, and fstream for both input and output. Member functions of these or base classes are used to perform I/O operations. Such operators and functions as <<, put(), and write() are used for output, while >>, get(), and read() are used for input.
The read() and write() functions work in binary mode, so entire objects can be saved to disk no matter what sort of data they contain. Single objects can be stored, as can arrays or other data structures of many objects. File I/O can be handled by member functions. This can be the responsibility of individual objects, or the class itself can handle I/O using static member functions.
A check for error conditions should be made after each file operation. The file object itself takes on a value of 0 if an error occurred. Also, several member functions can be used to determine specific kinds of errors. The extraction operator >> and the insertion operator << can be overloaded so that they work with programmer-defined data types. Memory can be considered a stream, and data sent to it as if it were a file.


Chapter 12
628
11.Mode bits such as app and ate
a.are defined in the ios class.
b.can specify if a file is open for reading or writing.
c.work with the put() and get() functions.
d.specify ways of opening a file.
12.Define what current position means when applied to files.
13.True or false: A file pointer always contains the address of the file.
14.Write a statement that moves the current position 13 bytes backward in a stream object called f1.
15.The statement
f1.write( (char*)&obj1, sizeof(obj1) );
a.writes the member functions of obj1 to f1.
b.writes the data in obj1 to f1.
c.writes the member functions and the data of obj1 to f1.
d.writes the address of obj1 to f1.
16.Command-line arguments are
a.disagreements in the military.
b.typed following a program name at the command prompt.
c.accessed through arguments to main().
d.accessible only from disk files.
17.Used with cin, what does the skipws flag accomplish?
18.Write a declarator for main() that will enable command-line arguments.
19.In console mode programs, the printer can be accessed using the predefined filename
________.
20.Write the declarator for the overloaded >> operator that takes output from an object of class istream and displays it as the contents of an object of class Sample.
Exercises
Answers to starred exercises can be found in Appendix G.
*1. Start with the Distance class from the ENGLCON example in Chapter 6, “Objects and Classes.” Using a loop similar to that in the DISKFUN example in this chapter, get a number of Distance values from the user, and write them to a disk file. Append them to existing values in the file, if any. When the user signals that no more values will be input, read the file and display all the values.


Chapter 12
630
6.Make a class called name from the data in Exercise 4 (first name, middle initial, last name, employee number). Create member functions for this class that read and write an object’s data to a disk file, using ofstream, and read it back using ifstream. Use formatted data with the << and >> operators. The read and write member functions should be self-contained: they should include statements to open the appropriate stream and read or write a record.
The write function can simply append its data to the end of the file. The read function will need a way to select which record it’s going to read. One way to do this is to call it with a parameter representing the record number. Once it knows which record it should
read, how does the read function find the record? You might think you could use the seekg() function, but that isn’t much help because in formatted I/O the records are all different lengths (depending on the number of characters in the strings and the number of digits in the integer). So you’ll need to actually read records until you’ve skipped forward to the one you want.
In main(), call these member functions to allow the user to enter data for a number of objects that are written to a file as they are entered. The program then displays all this data by reading it from the file.
7.Another approach to adding file stream I/O to an object is to make the file stream itself a static member of the object. Why do that? Well, it’s often conceptually easier to think of the stream as being related to the class as a whole than to the individual objects of the class. Also, it’s more efficient to open a stream only once, then read and write objects to it as needed. For example, once the file is opened, each time the read function is called it can return the data for the next object in the file. The file pointer will progress automatically through the file because the file is not closed between reads. Rewrite the program in Exercises 4 and 6 to use an fstream object as a static data item of the name class. Keep the same functionality that is in those exercises. Write a static function to open this stream, and another static function to reset the file pointer to the beginning of the file. You can use this reset function when you’re done writing and want to read all the records back from the file.
8.Starting with the LINKLIST program in Chapter 10, “Pointers,” create a program that gives the user four options, which can be selected by pressing a key.
•Add a link to the list in memory (the user supplies the data, which is one integer)
•Display the data from all the links in memory
•Write the data for all the links to a disk file (creating or truncating the file as necessary)
•Read all the data back from the file, and construct a new linked list in which to store it


Chapter 12
632
NOTE
Note: Don’t try to read a file generated with the EMPL_IO program. The classes are not the same because of the find() member function in the new program, and disaster will result if their data is mixed, as discussed in this chapter. You may need to turn on an Enable RTTI option in your compiler. Consult Appendix C, “Microsoft Visual C++,” or Appendix D, “Borland C++Builder,” as appropriate.


Chapter 13
634
In previous chapters we’ve seen how the various parts of a C++ program—such as class declarations, member functions, and a main() function—are combined. However, the programs in those chapters all consisted of a single file. Now let’s look at program organization from a wider perspective, involving multiple files. We’ll see how communication is carried out among files, and how header files fit into the picture.
Besides discussing multifile programs in general, this chapter will introduce some longer and more ambitious applications. Our aim in these programs is not that you necessarily understand every detail of their operation, but that you acquire a general understanding of how the elements of larger programs relate to one another. These programs also show how classes can be used in more realistic applications than the short examples we’ve seen so far. On the other hand, they are not so long that it takes all spring to wade through them.
Reasons for Multifile Programs
There are several reasons for using multifile programs. These include the use of class libraries, the organization of programmers working on a project, and the conceptual design of a program. Let’s reflect briefly on these issues.
Class Libraries
In traditional procedure-oriented languages it has long been customary for software vendors to furnish libraries of functions. Other programmers then combine these libraries with their own custom-written routines to create an application for the end user.
Libraries provide ready-made functions for a wide variety of fields. For instance, a vendor might supply a library of functions for handling statistics calculations, or one for advanced memory management.
Since C++ is organized around classes rather than functions, it’s not surprising that libraries for C++ programs consist of classes. What may be surprising is how much better a class library is than an old-fashioned function library. Because classes encapsulate both data and functions, and because they more closely model objects in real life, the interface between a class library and the application that makes use of it can be much cleaner than that provided by a function library.
For these reasons class libraries assume a more important role in C++ programming than function libraries do in traditional programming. A class library can take over a greater portion of the programming burden. An applications programmer, if the right class library is available, may find that only a minimal amount of programming is necessary to create a final product. Also, as more and more class libraries are created, the chances of finding one that solves your particular programming problem continues to increase.