1、 2012 年毕业设计论文英汉互译部分 Chapter 8. The IO Library In C+, input/output is provided through the library. The library defines a family of types that support IO to and from devices such as files and console windows. Additional types allow strings to act like files, which gives us a way to convert data to an
2、d from character forms without also doing IO. Each of these IO types defines how to read and write values of the built-in data types. In addition, class designers generally use the library IO facilities to read and write objects of the classes that they define. Class types are usually read and writt
3、en using the same operators and conventions that the IO library defines for the built-in types. This chapter introduces the fundamentals of the IO library. Later chapters will cover additional capabilities: Chapter 14 will look at how we can write our own input and output operators; Appendix A will
4、cover ways to control formatting and random access to files. Our programs have already used many IO library facilities: istream (input stream) type, which supports input operations ostream (output stream) type, which provides output operations cin (pronounced see-in) an istream object that reads the
5、 standard input. cout (pronounced see-out) an ostream object that writes to the standard output cerr (pronounced see-err) an ostream object that writes to the standard error. cerr is usually used for program error messages. operator , which is used to read input from an istream object operator to re
6、ad data regardless of whether we re reading a console window, a disk file, or an in-memory string. Similarly, we d like to use that operator regardless of whether the characters we read fit in a char or require the wchar_t(Section2.1.1,p.34) type. At first glance, the complexities involved in suppor
7、ting or using these different kinds of devices and different sized character streams might seem a daunting problem. To manage the complexity, the library uses inheritance to define a set of object-oriented classes. Well have more to say about inheritance and object-oriented programming in Part IV, b
8、ut generally speaking, types related by inheritance share a common interface. When one class inherits from another, we (usually) can use the same operations on both classes. More specifically, when two types are related by inheritance, we say that one class inherits the behavior the interface of its
9、 parent. In C+ we speak of the parent as the base class and the inheriting class as a derived class. The IO types are defined in three separate headers: iostream defines the types used to read and write to a console window, fstream defines the types used to read and write named files, and sstream de
10、fines the types used to read and write in-memory strings. Each of the types in fstream and sstream is derived from a corresponding type defined in the iostream header. Table 8.1 lists the IO classes and Figure 8.1 on the next page illustrates the inheritance relationships among these types. Inherita
11、nce is usually illustrated similarly to how a family tree is displayed. The topmost circle represents a base (or parent) class. Lines connect a base class to its derived (or children) class(es). So, for example, this figure indicates that istream is the base class of ifstream and istringstream. It i
12、s also the base class for iostream, which in turn is the base class for sstream and fstream classes. Because the types ifstream and istringstream inherit from istream, we already know a great deal about how to use these types. Each program weve written that read an istream could be used to read a fi
13、le (using the ifstream type) or a string (using the istringstream type). Similarly, programs that did output could use an ofstream or ostringstream instead of ostream. In addition to the istream and ostream types, the iostream header also defines the iostream type. Although our programs have not use
14、d this type, we actually know a good bit about how to use an iostream. The iostream type is derived from both istream and ostream. Being derived from both types means that an iostream object shares the interface of both its parent types. That is, we can use an iostream type to do both input and outp
15、ut to the same stream. The library also defines two types that inherit from iostream. These types can be used to read or write to a file or a string. Using inheritance for the IO types has another important implication: As well see in Chapter 15, when we have a function that takes a reference to a b
16、ase-class type, we can pass an object of a derived type to that function. This fact means that a function written to operate on istream& can be called with an ifstream or istring stream object. Similarly,a function that takes an ostream& can be called with an ofstream or ostring stream object. Becau
17、se the IO types are related by inheritance, we can write one function and apply it to all three kinds of streams: console, disk files, or string streams. International Character Support The stream classes described thus far read and write streams composed of type char. The library defines a correspo
18、nding set of types supporting the wchar_t type. Each class is distinguished from its char counterpart by a w prefix. Thus, the types wostream, wistream, and wiostream read and write wchar_t data to or from a console window. The file input and output classes are wifstream, wofstream, and wfstream. Th
19、e wchar_t versions of string stream input and output are wistring stream, wostring stream, and wstring stream. The library also defines objects to read and write wide characters from the standard input and standard output. These objects are distinguished from the char counterparts by a w prefix: The
20、 wchar_t standard input object is named wcin; standard output is wcout; and standard error is wcerr. Each of the IO headers defines both the char and wchar_t classes and standard input/output objects. The stream-based wchar_t classes and objects are defined in iostream, the wide character file strea
21、m types in fstream, and the wide character string streams in sstream. No Copy or Assign for IO Objects For reasons that will be more apparent when we study classes and inheritance in Parts III and IV, the library types do not allow allow copy or assignment. This requirement has two particularly important implications. As well see in Chapter 9, only element types that support copy can be stored