C++毕业论文外文翻译.doc_第1页
C++毕业论文外文翻译.doc_第2页
C++毕业论文外文翻译.doc_第3页
C++毕业论文外文翻译.doc_第4页
C++毕业论文外文翻译.doc_第5页
免费预览已结束,剩余4页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

毕业设计论文英汉互译部分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 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 written 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 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 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 read data 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 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, but 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 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 defines 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. inheritance 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 is 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 file (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 used 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 output 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 base-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. because 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 supportthe stream classes described thus far read and write streams composed of type char. the library defines a corresponding 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. the 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 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 stream types in fstream, 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. 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 that 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 state, 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 applies 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 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 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 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 the 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. for 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 type 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 values 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 continue 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 also 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 true.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 specified 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 of 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 single 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 对象能够像文件一样操作,从而使我们无须 io 就能实现数据与字符之间的转换。这些 io 类型都定义了如何读写内置数据类型的值。此外,一般来说,类的设计者还可以很方便地使用 io 标准库设施读写自定义类的对象。类类型通常使用 io 标准库为内置类型定义的操作符和规则来进行读写。本章将介绍 io标准库的基础知识,而更多的内容会在后续章节中介绍:第十四章考虑如何编写自己的输入输出操作符:附录 a 则介绍格式控制以及文件的随机访问。前面的程序已经使用了多种 io 标准库提供的 istream (输入流)类型,提供输入操作。 ostream (输出流)类型,提供输出操作。 cin (发音为 see-in):读入标准输入的 istream 对象。 cout (发音为 see-out):写到标准输出的 ostream 对象。 cerr (发音为 see-err):输出标准错误的 ostream 对象。cerr 常用于程序错误信息。 操作符,用于从 istream 对象中读入输入。 操作符。相似地,无论我们读的是 char 类型的字符还是 wchar_t(第 2.1.1 节)的字符,也都可以使用该操作符。乍看起来,要同时支持或使用不同类型设备以及不同大小的字符流,其复杂程度似乎相当可怕。为了管理这样的复杂性,标准库使用了继承(inheritance)来定义一组面向对象(object-oriented)类。在本书的第四部分将会更详细地讨论继承和面向对象程序设计,不过,一般而言,通过继承关联起来的类型都共享共同的接口。当一个类继承另一个类时,这两个类通常可以使用相同的操作。更确切地说,如果两种类型存在继承关系,则可以说一个类“继承”了其父类的行为接口。c+ 中所提及的父类称为基类(base class),而继承而来的类则称为派生类(derived class)。io 类型在三个独立的头文件中定义:iostream 定义读写控制窗口的类型,fstream 定义读写已命名文件的类型,而 sstream 所定义的类型则用于读写存储在内存中的 string 对象。在 fstream 和 sstream 里定义的每种类型都是从 iostream 头文件中定义的相关类型派生而来。表 8.1 列出了 c+ 的 io 类,而图 8.1 则阐明这些类型之间的继承关系。继承关系通常可以用类似于家庭树的图解说明。最顶端的圆圈代表基类(或称“父类”),基类和派生类(或称“子类”)之间用线段连接。因此,图 8.1 所示,istream 是 ifstream 和 istringstream 的基类,同时也是 iostream 的基类,而 iostream 则是 stringstream 和 fstream 的基类。由于 ifstream 和 istringstream 类型继承了 istream 类,因此已知这两种类型的大量用法。我们曾经编写过的读 istream 对象的程序也可用于读文件(使用 ifstream 类型)或者 string 对象(使用 istringstream 类型)。类似地,提供输出功能的程序同样可用 ofstream 或 ostringstream 取代 ostream 类型实现。除了 istream 和 ostream 类型之外,iostream 头文件还定义了 iostream 类型。尽管我们的程序还没用过这种类型,但事实上可以多了解一些关于 iostream 的用法。iostream 类型由 istream 和 ostream 两者派生而来。这意味着 iostream 对象共享了它的两个父类的接口。也就是说,可使用 iostream 类型在同一个流上实现输入和输出操作。标准库还定义了另外两个继承 iostream 的类型。这些类型可用于读写文件或 string 对象。对 io 类型使用继承还有另外一个重要的含义:正如在第十五章可以看到的,如果函数有基类类型的引用形参时,可以给函数传递其派生类型的对象。这就意味着:对 istream& 进行操作的函数,也可使用 ifstream 或者 istringstream 对象来调用。类似地,形参为 ostream& 类型的函数也可用 ofstream 或者 ostringstream 对象调用。因为 io 类型通过继承关联,所以可以只编写一个函数,而将它应用到三种类型的流上:控制台、磁盘文件或者字符串流(string streams)。国际字符的支持迄今为止,所描述的流类(stream class)读写的是由 char 类型组成的流。此外,标准库还定义了一组相关的类型,支持 wchar_t 类型。每个类都加上“w”前缀,以此与 char 类型的版本区分开来。于是,wostream、wistream 和 wiostream 类型从控制窗口读写 wchar_t 数据。相应的文件输入输出类是 wifstream、wofstream 和 wfstream。而 wchar_t 版本的 string 输入输出流则是 wistringstream、wostringstream 和 wstringstream。标准库还定义了从标准输入输出读写宽字符的对象。这些对象加上“w”前缀,以此与 char 类型版本区分:wchar_t 类型的标准输入对象是 wcin;标准输出是 wcout;而标准错误则是 wcerr。每一个 io 头文件都定义了 char 和 wchar_t 类型的类和标准输入输出对象。基于流的 wchar_t 类型的类和对象在 iostream 中定义,宽字符文件流类型在 fstream 中定义,而宽字符 stringstream 则在 sstream 头文件中定义。io 对象不可复制或赋值出于某些原因,标准库类型不允许做复制或赋值操作。其原因将在后面第三部分和第四部分学习类和继承时阐明。这个要求有两层特别重要的含义。正如在第九章看到的,只有支持复制的元素类型可以存储在 vector 或其他容器类型里。由于流对象不能复制,因此不能存储在 vector(或其他)容器中(即不存在存储流对象的 vector 或其他容器)。第二个含义是:形参或返回类型也不能为流类型。如果需要传递或返回 io 对象,则必须传递或返回指向该对象的指针或引用。一般情况下,如果要传递 io 对象以便对它进行读写,可用非 const 引用的方式传递这个流对象。对 io 对象的读写会改变它的状态,因此引用必须是非 const 的。8.2. condition states在展开讨论 f

温馨提示

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

评论

0/150

提交评论