C毕业论文外文翻译

上传人:无*** 文档编号:77945242 上传时间:2022-04-20 格式:DOC 页数:9 大小:90.52KB
收藏 版权申诉 举报 下载
C毕业论文外文翻译_第1页
第1页 / 共9页
C毕业论文外文翻译_第2页
第2页 / 共9页
C毕业论文外文翻译_第3页
第3页 / 共9页
资源描述:

《C毕业论文外文翻译》由会员分享,可在线阅读,更多相关《C毕业论文外文翻译(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 类,因此已知这两种类型的大量用法。我们曾经

43、编写过的读 istream 对象的程序也可用于读文件(使用 ifstream 类型)或者 string 对象(使用 istringstream 类型)。类似地,提供输出功能的程序同样可用 ofstream 或 ostringstream 取代 ostream 类型实现。除了 istream 和 ostream 类型之外,iostream 头文件还定义了 iostream 类型。尽管我们的程序还没用过这种类型,但事实上可以多了解一些关于 iostream 的用法。iostream 类型由 istream 和 ostream 两者派生而来。这意味着 iostream 对象共享了它的两个父类的接口。

44、也就是说,可使用 iostream 类型在同一个流上实现输入和输出操作。标准库还定义了另外两个继承 iostream 的类型。这些类型可用于读写文件或 string 对象。对 IO 类型使用继承还有另外一个重要的含义:正如在第十五章可以看到的,如果函数有基类类型的引用形参时,可以给函数传递其派生类型的对象。这就意味着:对 istream& 进行操作的函数,也可使用 ifstream 或者 istringstream 对象来调用。类似地,形参为 ostream& 类型的函数也可用 ofstream 或者 ostringstream 对象调用。因为 IO 类型通过继承关联,所以可以只编写一个函数,

45、而将它应用到三种类型的流上:控制台、磁盘文件或者字符串流(string streams)。国际字符的支持迄今为止,所描述的流类(stream class)读写的是由 char 类型组成的流。此外,标准库还定义了一组相关的类型,支持 wchar_t 类型。每个类都加上“w”前缀,以此与 char 类型的版本区分开来。于是,wostream、wistream 和 wiostream 类型从控制窗口读写 wchar_t 数据。相应的文件输入输出类是 wifstream、wofstream 和 wfstream。而 wchar_t 版本的 string 输入输出流则是 wistringstream、w

46、ostringstream 和 wstringstream。标准库还定义了从标准输入输出读写宽字符的对象。这些对象加上“w”前缀,以此与 char 类型版本区分:wchar_t 类型的标准输入对象是 wcin;标准输出是 wcout;而标准错误则是 wcerr。每一个 IO 头文件都定义了 char 和 wchar_t 类型的类和标准输入输出对象。基于流的 wchar_t 类型的类和对象在 iostream 中定义,宽字符文件流类型在 fstream 中定义,而宽字符 stringstream 则在 sstream 头文件中定义。IO 对象不可复制或赋值出于某些原因,标准库类型不允许做复制或赋

47、值操作。其原因将在后面第三部分和第四部分学习类和继承时阐明。这个要求有两层特别重要的含义。正如在第九章看到的,只有支持复制的元素类型可以存储在 vector 或其他容器类型里。由于流对象不能复制,因此不能存储在 vector(或其他)容器中(即不存在存储流对象的 vector 或其他容器)。第二个含义是:形参或返回类型也不能为流类型。如果需要传递或返回 IO 对象,则必须传递或返回指向该对象的指针或引用。一般情况下,如果要传递 IO 对象以便对它进行读写,可用非 const 引用的方式传递这个流对象。对 IO 对象的读写会改变它的状态,因此引用必须是非 const 的。8.2. Conditi

48、on States在展开讨论 fstream 和 sstream 头文件中定义的类型之前,需要了解更多 IO 标准库如何管理其缓冲区及其流状态的相关内容。谨记本节和下一节所介绍的内容同样适用于普通流、文件流以及 string 流。实现 IO 的继承正是错误发生的根源。一些错误是可恢复的;一些错误则发生在系统底层,位于程序可修正的范围之外。IO 标准库管理一系列条件状态(condition state)成员,用来标记给定的 IO 对象是否处于可用状态,或者碰到了哪种特定的错误。表 8.2 列出了标准库定义的一组函数和标记,提供访问和操纵流状态的手段。如果在标准输入设备输入 Borges,则 ci

49、n 在尝试将输入的字符串读为 int 型数据失败后,会生成一个错误状态。类似地,如果输入文件结束符(end-of-file),cin 也会进入错误状态。而如果输入 1024,则成功读取,cin 将处于正确的无错误状态。流必须处于无错误状态,才能用于输入或输出。检测流是否用的最简单的方法是检查其真值。if 语句直接检查流的状态,而 while 语句则检测条件表达式返回的流,从而间接地检查了流的状态。如果成功输入,则条件检测为 true。条件状态许多程序只需知道是否有效。而某些程序则需要更详细地访问或控制流的状态,此时,除了知道流处于错误状态外,还必须了解它遇到了哪种类型的错误。例如,程序员也许希

50、望弄清是到达了文件的结尾,还是遇到了 IO 设备上的错误。所有流对象都包含一个条件状态成员,该成员由 setstate 和 clear 操作管理。这个状态成员为 iostate 类型,这是由各个 iostream 类分别定义的机器相关的整型。该状态成员以二进制位(bit)的形式使用,类似于第 5.3.1 节的例子中用于记录测验成绩的 int_quiz1 变量。每个 IO 类还定义了三个 iostate 类型的常量值,分别表示特定的位模式。这些常量值用于指出特定类型的 IO 条件,可与位操作符(第 5.3 节)一起使用,以便在一次操作中检查或设置多个标志。badbit 标志着系统级的故障,如无法

51、恢复的读写错误。如果出现了这类错误,则该流通常就不能再继续使用了。如果出现的是可恢复的错误,如在希望获得数值型数据时输入了字符,此时则设置 failbit 标志,这种导致设置 failbit 的问题通常是可以修正的。eofbit 是在遇到文件结束符时设置的,此时同时还设置了 failbit。流的状态由 bad、fail、eof 和 good 操作提示。如果 bad、fail 或者 eof 中的任意一个为 true,则检查流本身将显示该流处于错误状态。类似地,如果这三个条件没有一个为 true,则 good 操作将返回 true。clear 和 setstate 操作用于改变条件成员的状态。cl

52、ear 操作将条件重设为有效状态。在流的使用出现了问题并做出补救后,如果我们希望把流重设为有效状态,则可以调用 clear 操作。使用 setstate 操作可打开某个指定的条件,用于表示某个问题的发生。除了添加的标记状态,setstate 将保留其他已存在的状态变量不变。条件状态的访问rdstate 成员函数返回一个 iostate 类型值,该值对应于流当前的整个条件状态。多种状态的处理常常会出现需要设置或清除多个状态二进制位的情况。此时,可以通过多次调用 setstate 或者 clear 函数实现。另外一种方法则是使用按位或(OR)操作符(第 5.3 节)在一次调用中生成“传递两个或更多状态位”的值。按位或操作使用其操作数的二进制位模式产生一个整型数值。对于结果中的每一个二进制位,如果其值为 1,则该操作的两个操作数中至少有一个的对应二进制位是 1。

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!