C++畢業(yè)論文外文翻譯_第1頁
C++畢業(yè)論文外文翻譯_第2頁
C++畢業(yè)論文外文翻譯_第3頁
C++畢業(yè)論文外文翻譯_第4頁
C++畢業(yè)論文外文翻譯_第5頁
已閱讀5頁,還剩4頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、2012年畢業(yè)設(shè)計論文英漢互譯部分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 th

5、at 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

6、 from an istream object· operator <<, which is used to write output to an ostream object· getline function, which takes a reference to an istream and a reference to a string and reads a word from the istream into the stringThis chapter looks briefly at some additional IO operations,

7、and discusses support for reading and writing files and strings. Appendix A covers how to control formatting of IO operations, support for random access to files, and support for unformatted IO. This primer does not describe the entire iostream libraryin particular, we do not cover the system-specif

8、ic implementation details, nor do we discuss the mechanisms by which the library manages input and output buffers or how we might write our own buffer classes. These topics are beyond the scope of this book. Instead, we'll focus on those portions of the IO library that are most useful in ordinar

9、y programs.8.1. An Object-Oriented LibraryThe IO types and objects weve used so far read and write streams of data and are used to interact with a users console window. Of course, real programs cannot be limited to doing IO solely to or from a console window. Programs often need to read or write nam

10、ed files. Moreover, it can be quite convenient to use the IO operations to format data in memory, thereby avoiding the complexity and run-time expense of reading or writing to a disk or other device. Applications also may have to read and write languages that require wide-character support. Conceptu

11、ally, neither the kind of the character size affect the IO operations we want to perform. For example, wed like to use >> 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 ch

12、aracters we read fit in a char or require the wchar_t(Section) 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

13、 a set of object-oriented classes. We'll 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. M

14、ore 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 de

15、fines 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 head

16、er. lists the IO classes and 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) c

17、lass(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

18、great deal about how to use these types. Each program we've 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 t

19、o 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 iostre

20、am 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 typ

21、es has another important implication: As we'll 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 is

22、tring 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 Ch

23、aracter 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 wi

24、ostream 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 wri

25、te 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 an

26、d 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

27、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 we'll see in Chapter 9, only element types that support copy can be stored in vectors or other container types. Because we c

28、annot 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 referen

29、ce.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

30、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

31、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

32、 functions and flags, listed in , 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 sta

33、te 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 t

34、he 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 acce

35、ss 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 conta

36、ins 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 sco

37、res in the example in (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 (, p. 154) to test or set multiple flags in one

38、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 possib

39、le 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 i

40、tself 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 aft

41、er 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).

42、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 funct

43、ions. Alternatively, we could use the bitwise OR (, 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 eithe

44、r of its operands.第八章 標準 IO 庫C+ 的輸入輸出(input/output)由標準庫提供。標準庫定義了一族類型,支持對文件和控制窗口等設(shè)備的讀寫(IO)。還定義了其他一些類型,使 string 對象能夠像文件一樣操作,從而使我們無須 IO 就能實現(xiàn)數(shù)據(jù)與字符之間的轉(zhuǎn)換。這些 IO 類型都定義了如何讀寫內(nèi)置數(shù)據(jù)類型的值。此外,一般來說,類的設(shè)計者還可以很方便地使用 IO 標準庫設(shè)施讀寫自定義類的對象。類類型通常使用 IO 標準庫為內(nèi)置類型定義的操作符和規(guī)則來進行讀寫。本章將介紹 IO標準庫的基礎(chǔ)知識,而更多的內(nèi)容會在后續(xù)章節(jié)中介紹:第十四章考慮如何編寫自己的輸入輸出操作

45、符:附錄 A 則介紹格式控制以及文件的隨機訪問。前面的程序已經(jīng)使用了多種 IO 標準庫提供的· istream (輸入流)類型,提供輸入操作。· ostream (輸出流)類型,提供輸出操作。· cin (發(fā)音為 see-in):讀入標準輸入的 istream 對象。· cout (發(fā)音為 see-out):寫到標準輸出的 ostream 對象。· cerr (發(fā)音為 see-err):輸出標準錯誤的 ostream 對象。cerr 常用于程序錯誤信息。· >> 操作符,用于從 istream 對象中讀入輸入。·

46、 << 操作符,用于把輸出寫到 ostream 對象中。· getline函數(shù),需要分別取 istream 類型和 string 類型的兩個引用形參,其功能是從 istream 對象讀取一個單詞,然后寫入 string 對象中。本章簡要地介紹一些附加的 IO 操作,并討論文件對象和 string 對象的讀寫。附錄 A 會介紹如何控制 IO 操作的格式、文件的隨機訪問以及無格式的 IO。本書是初級讀本,因此不會詳細討論完整的 iostream 標準庫特別是,我們不但沒有涉及系統(tǒng)特定的實現(xiàn)細則,也不討論標準庫管理輸入輸出緩沖區(qū)的機制,以及如何編寫自定義的緩沖區(qū)類。這些話題已超

47、出了本書的范疇。相對而言,本書把重點放在 IO 標準庫對普通程序最有用的部分。8.1. 面向?qū)ο蟮臉藴蕩炱駷橹梗覀円呀?jīng)使用 IO 類型和對象讀寫數(shù)據(jù)流,它們常用于與用戶控制窗口的交互。當(dāng)然,實際的程序不能僅限于對控制窗口的 IO,通常還需要讀或?qū)懸衙奈募?。此外,程序還應(yīng)該能方便地使用 IO 操作格式化內(nèi)存中的數(shù)據(jù),從而避免讀寫磁盤或其他設(shè)備的復(fù)雜性和運行代價。應(yīng)用程序還需要支持寬字符(wide-character)語言的讀寫。從概念上看,無論是設(shè)備的類型還是字符的大小,都不影響需要執(zhí)行的 IO 操作。例如,不管我們是從控制窗口、磁盤文件或內(nèi)存中的字符串讀入數(shù)據(jù),都可使用 >>

48、; 操作符。相似地,無論我們讀的是 char 類型的字符還是 wchar_t(第 2.1.1 節(jié))的字符,也都可以使用該操作符。乍看起來,要同時支持或使用不同類型設(shè)備以及不同大小的字符流,其復(fù)雜程度似乎相當(dāng)可怕。為了管理這樣的復(fù)雜性,標準庫使用了繼承(inheritance)來定義一組面向?qū)ο螅╫bject-oriented)類。在本書的第四部分將會更詳細地討論繼承和面向?qū)ο蟪绦蛟O(shè)計,不過,一般而言,通過繼承關(guān)聯(lián)起來的類型都共享共同的接口。當(dāng)一個類繼承另一個類時,這兩個類通常可以使用相同的操作。更確切地說,如果兩種類型存在繼承關(guān)系,則可以說一個類“繼承”了其父類的行為接口。C+ 中所提及的父類

49、稱為基類(base class),而繼承而來的類則稱為派生類(derived class)。IO 類型在三個獨立的頭文件中定義:iostream 定義讀寫控制窗口的類型,fstream 定義讀寫已命名文件的類型,而 sstream 所定義的類型則用于讀寫存儲在內(nèi)存中的 string 對象。在 fstream 和 sstream 里定義的每種類型都是從 iostream 頭文件中定義的相關(guān)類型派生而來。 列出了 C+ 的 IO 類,而 則闡明這些類型之間的繼承關(guān)系。繼承關(guān)系通常可以用類似于家庭樹的圖解說明。最頂端的圓圈代表基類(或稱“父類”),基類和派生類(或稱“子類”)之間用線段連接。因此,

50、所示,istream 是 ifstream 和 istringstream 的基類,同時也是 iostream 的基類,而 iostream 則是 stringstream 和 fstream 的基類。由于 ifstream 和 istringstream 類型繼承了 istream 類,因此已知這兩種類型的大量用法。我們曾經(jīng)編寫過的讀 istream 對象的程序也可用于讀文件(使用 ifstream 類型)或者 string 對象(使用 istringstream 類型)。類似地,提供輸出功能的程序同樣可用 ofstream 或 ostringstream 取代 ostream 類型實現(xiàn)。除

51、了 istream 和 ostream 類型之外,iostream 頭文件還定義了 iostream 類型。盡管我們的程序還沒用過這種類型,但事實上可以多了解一些關(guān)于 iostream 的用法。iostream 類型由 istream 和 ostream 兩者派生而來。這意味著 iostream 對象共享了它的兩個父類的接口。也就是說,可使用 iostream 類型在同一個流上實現(xiàn)輸入和輸出操作。標準庫還定義了另外兩個繼承 iostream 的類型。這些類型可用于讀寫文件或 string 對象。對 IO 類型使用繼承還有另外一個重要的含義:正如在第十五章可以看到的,如果函數(shù)有基類類型的引用形參

52、時,可以給函數(shù)傳遞其派生類型的對象。這就意味著:對 istream& 進行操作的函數(shù),也可使用 ifstream 或者 istringstream 對象來調(diào)用。類似地,形參為 ostream& 類型的函數(shù)也可用 ofstream 或者 ostringstream 對象調(diào)用。因為 IO 類型通過繼承關(guān)聯(lián),所以可以只編寫一個函數(shù),而將它應(yīng)用到三種類型的流上:控制臺、磁盤文件或者字符串流(string streams)。國際字符的支持迄今為止,所描述的流類(stream class)讀寫的是由 char 類型組成的流。此外,標準庫還定義了一組相關(guān)的類型,支持 wchar_t 類型。每

53、個類都加上“w”前綴,以此與 char 類型的版本區(qū)分開來。于是,wostream、wistream 和 wiostream 類型從控制窗口讀寫 wchar_t 數(shù)據(jù)。相應(yīng)的文件輸入輸出類是 wifstream、wofstream 和 wfstream。而 wchar_t 版本的 string 輸入輸出流則是 wistringstream、wostringstream 和 wstringstream。標準庫還定義了從標準輸入輸出讀寫寬字符的對象。這些對象加上“w”前綴,以此與 char 類型版本區(qū)分:wchar_t 類型的標準輸入對象是 wcin;標準輸出是 wcout;而標準錯誤則是 wcerr。每一個 IO 頭文件都定義了 char 和 wchar_t 類型的類和標準輸入輸出對象。基于流的 wchar_t 類型的類和對象在 iostream 中定義,寬字符文件流類型在 fstream 中定義,而寬字符 stringstream 則在 sstream 頭文件中定義。IO 對象不可復(fù)制或賦值出于某些原因,標準庫類型不允許做復(fù)制或賦值操作。其原因?qū)⒃诤竺娴谌糠趾偷谒牟糠謱W(xué)習(xí)類和繼承時闡明。這個要求有兩層特別重要的含義。正如在第九章看到的,只有支持復(fù)制的元素類型可以存儲在 vector 或其他容

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論