C++毕业论文外文翻译.doc

上传人:小小飞 文档编号:3901233 上传时间:2019-10-09 格式:DOC 页数:9 大小:90.53KB
返回 下载 相关 举报
C++毕业论文外文翻译.doc_第1页
第1页 / 共9页
C++毕业论文外文翻译.doc_第2页
第2页 / 共9页
C++毕业论文外文翻译.doc_第3页
第3页 / 共9页
C++毕业论文外文翻译.doc_第4页
第4页 / 共9页
C++毕业论文外文翻译.doc_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《C++毕业论文外文翻译.doc》由会员分享,可在线阅读,更多相关《C++毕业论文外文翻译.doc(9页珍藏版)》请在三一文库上搜索。

1、2012年毕业设计论文英汉互译部分Chapter 8. The IO LibraryIn 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 and fr

2、om 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 written u

3、sing 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 cover

4、 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 stand

5、ard 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 read dat

6、a regardless of whether were reading a console window, a disk file, or an in-memory string. Similarly, wed 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 supporting or u

7、sing 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, but genera

8、lly 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 parent.

9、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 defines the

10、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. Inheritance is usu

11、ally 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 is also the

12、 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 file (using t

13、he 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 used this type

14、, 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 output to the s

15、ame 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 base-class ty

16、pe, 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. Because the IO ty

17、pes 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 SupportThe stream classes described thus far read and write streams composed of type char. The library defines a corresponding set of t

18、ypes 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. The wchar_t vers

19、ions 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 wchar_t stand

20、ard 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 stream types in fstr

21、eam, and the wide character string streams in sstream.No Copy or Assign for IO ObjectsFor 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

22、. As well see in Chapter 9, only element types that support copy can be stored in vectors or other container types. Because we cannot copy stream objects, we cannot have a vector (or other container) that holds stream objects.The second implication is that we cannot have a parameter or return type t

23、hat is one of the stream types. If we need to pass or return an IO object, it must be passed or returned as a pointer or reference.Typically, we pass a stream as a nonconst reference because we pass an IO object intending to read from it or write to it. Reading or writing an IO object changes its st

24、ate, so the reference must be nonconst.8.2. Condition StatesBefore we explore the types defined in fstream and sstream, we need to understand a bit more about how the IO library manages its buffers and the state of a stream. Keep in mind that the material we cover in this section and the next applie

25、s equally to plain streams, file streams, or string streams.Inherent in doing IO is the fact that errors can occur. Some errors are recoverable; others occur deep within the system and are beyond the scope of a program to correct. The IO library manages a set of condition state members that indicate

26、 whether a given IO object is in a usable state or has encountered a particular kind of error. The library also defines a set of functions and flags, listed in Table 8.2, that give us access to and let us manipulate the state of each stream.If we enter Borges on the standard input, then cin will be

27、put in an error state following the unsuccessful attempt to read a string of characters as an int. Similarly, cin will be in an error state if we enter an end-of-file. Had we entered 1024, then the read would be successful and cin would be in a good, non-error state.To be used for input or output, a

28、 stream must be in a non-error state. The easiest way to test whether a stream is okay is to test its truth value.The if directly tests the state of the stream. The while does so indirectly by testing the stream returned from the expression in the condition. If that input operation succeeds, then th

29、e condition tests true.Condition StatesMany programs need only know whether a stream is valid. Other programs need more fine-grained access to and control of the state of the stream. Rather than knowing that the stream is in an error state, we might want to know what kind of error was encountered. F

30、or example, we might want to distinguish between reaching end-of-file and encountering an error on the IO device.Each stream object contains a condition state member that is managed through the setstate and clear operations. This state member has type iostate, which is a machine-dependent integral t

31、ype defined by each iostream class. It is used as a collection of bits, much the way we used the int_quiz1 variable to represent test scores in the example in Section 5.3.1 (p. 156).Each IO class also defines three const values of type iostate that represent particular bit patterns. These const valu

32、es are used to indicate particular kinds of IO conditions. They can be used with the bitwise operators (Section 5.3, p. 154) to test or set multiple flags in one operation.The badbit indicates a system level failure, such as an unrecoverable read or write error. It is usually not possible to continu

33、e using a stream after such an error. The failbit is set after a recoverable error, such as reading a character when numeric data was expected. It is often possible to correct the problem that caused the failbit to be set. The eofbit is set when an end-of-file is encountered. Hitting end-of-file als

34、o sets the failbit.The state of the stream is revealed by the bad, fail, eof, and good operations. If any of bad, fail, or eof are true, then testing the stream itself will indicate that the stream is in an error state. Similarly, the good operation returns TRue if none of the other conditions is tr

35、ue.The clear and setstate operations change the state of the condition member. The clear operations put the condition back in its valid state. They are called after we have remedied whatever problem occurred and we want to reset the stream to its valid state. The setstate operation turns on the spec

36、ified condition to indicate that a problem occurred. setstate leaves the existing state variables unchanged except that it adds the additional indicated state(s).Accessing the Condition StateThe rdstate member function returns an iostate value that corresponds to the entire current condition state o

37、f the stream.Dealing with Multiple StatesOften we need to set or clear multiple state bits. We could do so by making multiple calls to the setstate or clear functions. Alternatively, we could use the bitwise OR (Section 5.3, p. 154) operator to generate a value to pass two or more state bits in a si

38、ngle call. The bitwise OR generates an integral value using the bit patterns of its operands. For each bit in the result, the bit is 1 if the corresponding bit is 1 in either of its operands.第八章 标准 IO 库C+ 的输入输出(input/output)由标准库提供。标准库定义了一族类型,支持对文件和控制窗口等设备的读写(IO)。还定义了其他一些类型,使 string 对象能够像文件一样操作,从而使我们

39、无须 IO 就能实现数据与字符之间的转换。这些 IO 类型都定义了如何读写内置数据类型的值。此外,一般来说,类的设计者还可以很方便地使用 IO 标准库设施读写自定义类的对象。类类型通常使用 IO 标准库为内置类型定义的操作符和规则来进行读写。本章将介绍 IO标准库的基础知识,而更多的内容会在后续章节中介绍:第十四章考虑如何编写自己的输入输出操作符:附录 A 则介绍格式控制以及文件的随机访问。前面的程序已经使用了多种 IO 标准库提供的 istream (输入流)类型,提供输入操作。 ostream (输出流)类型,提供输出操作。 cin (发音为 see-in):读入标准输入的 istream

40、 对象。 cout (发音为 see-out):写到标准输出的 ostream 对象。 cerr (发音为 see-err):输出标准错误的 ostream 对象。cerr 常用于程序错误信息。 操作符,用于从 istream 对象中读入输入。 操作符。相似地,无论我们读的是 char 类型的字符还是 wchar_t(第 2.1.1 节)的字符,也都可以使用该操作符。乍看起来,要同时支持或使用不同类型设备以及不同大小的字符流,其复杂程度似乎相当可怕。为了管理这样的复杂性,标准库使用了继承(inheritance)来定义一组面向对象(object-oriented)类。在本书的第四部分将会更详细

41、地讨论继承和面向对象程序设计,不过,一般而言,通过继承关联起来的类型都共享共同的接口。当一个类继承另一个类时,这两个类通常可以使用相同的操作。更确切地说,如果两种类型存在继承关系,则可以说一个类“继承”了其父类的行为接口。C+ 中所提及的父类称为基类(base class),而继承而来的类则称为派生类(derived class)。IO 类型在三个独立的头文件中定义:iostream 定义读写控制窗口的类型,fstream 定义读写已命名文件的类型,而 sstream 所定义的类型则用于读写存储在内存中的 string 对象。在 fstream 和 sstream 里定义的每种类型都是从 io

42、stream 头文件中定义的相关类型派生而来。表 8.1 列出了 C+ 的 IO 类,而图 8.1 则阐明这些类型之间的继承关系。继承关系通常可以用类似于家庭树的图解说明。最顶端的圆圈代表基类(或称“父类”),基类和派生类(或称“子类”)之间用线段连接。因此,图 8.1 所示,istream 是 ifstream 和 istringstream 的基类,同时也是 iostream 的基类,而 iostream 则是 stringstream 和 fstream 的基类。由于 ifstream 和 istringstream 类型继承了 istream 类,因此已知这两种类型的大量用法。我们曾经编写过的读 istream 对象的程序也可用于读文件(使用 ifstream 类型)或者 string 对象(使用 is

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1