2023年全國碩士研究生考試考研英語一試題真題(含答案詳解+作文范文)_第1頁
已閱讀1頁,還剩8頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、<p>  2012年畢業(yè)設計論文英漢互譯部分</p><p>  Chapter 8. The IO Library</p><p>  In C++, input/output is provided through the library. The library defines a family of types that support IO to and from d

2、evices 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 re

3、ad 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 t</p><p>  This chapter introduces the fundamenta

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

5、files.</p><p>  Our programs have already used many IO library facilities:</p><p>  istream (input stream) type, which supports input operations</p><p>  ostream (output stream) typ

6、e, which provides output operations</p><p>  cin (pronounced see-in) an istream object that reads the standard input.</p><p>  cout (pronounced see-out) an ostream object that write

7、s to the standard output</p><p>  cerr (pronounced see-err) an ostream object that writes to the standard error. cerr is usually used for program error messages.</p><p>  operator >>, whic

8、h is used to read input from an istream object</p><p>  operator <<, which is used to write output to an ostream object</p><p>  getline function, which takes a reference to an istream and

9、 a reference to a string and reads a word from the istream into the string</p><p>  This chapter looks briefly at some additional IO operations, and discusses support for reading and writing files and string

10、s. 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

11、 the system-specific implementation details, nor do we discuss the mechanisms by which the library manages input and output buffers or how we might write our</p><p>  8.1. An Object-Oriented Library</p>

12、;<p>  The IO types and objects we’ve used so far read and write streams of data and are used to interact with a user’s console window. Of course, real programs cannot be limited to doing IO solely to or from a co

13、nsole window. Programs often need to read or write named 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 w

14、riting to a disk or other device. Applications also may have to read and w</p><p>  Conceptually, neither the kind of the character size affect the IO operations we want to perform. For example, we’d like to

15、 use >> to read 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 o

16、r require the wchar_t(Section2.1.1,p.34) type.</p><p>  At first glance, the complexities involved in supporting or using these different kinds of devices and different sized character streams might seem a d

17、aunting problem. To manage the complexity, the library uses inheritance to define a set of object-oriented classes. We'll have more to say about inheritance and object-oriented programming in Part IV, but generally s

18、peaking, types related by inheritance share a common interface. When one class inherits from another, we (usually) can use the same o</p><p>  The IO types are defined in three separate headers: iostream def

19、ines 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 fstrea

20、m 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. Inheritan<

21、/p><p>  Because the types ifstream and istringstream inherit from istream, we already know a great deal about how to use these types. Each program we've written that read an istream could be used to read a

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

23、 iostream header also defines the iostream type. Although our programs have not use</p><p>  Using inheritance for the IO types has another important implication: As we'll see in Chapter 15, when we have

24、 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 istr

25、ing 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 ca</p><p>  International Chara

26、cter Support</p><p>  The 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 distingui

27、shed 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, wofstr

28、eam, and wfstream. The wchar_t versions of string stream input and output are wistring stream, wostring s</p><p>  Each of the IO headers defines both the char and wchar_t classes and standard input/output o

29、bjects. 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.</p><p>  No Copy or Assign for

30、 IO Objects</p><p>  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.</p><p>  This requ

31、irement 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 cannot copy stream objects, we cannot

32、 have a vector (or other container) that holds stream objects.</p><p>  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

33、an IO object, it must be passed or returned as a pointer or reference.</p><p>  Typically, we pass a stream as a nonconst reference because we pass an IO object intending to read from it or write to it. Read

34、ing or writing an IO object changes its state, so the reference must be nonconst.</p><p>  8.2. Condition States</p><p>  Before we explore the types defined in fstream and sstream, we need to u

35、nderstand 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

36、streams.</p><p>  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 man

37、ages 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

38、, that give us access to and let us manipulate the state of each stream.</p><p>  If we enter Borges on the standard input, then cin will be put in an error state following the unsuccessful attempt to read a

39、 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.</p><p&g

40、t;  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.</p><p>  The if directly tests the state of the stream.

41、 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.</p><p>  Condition States</p><p&

42、gt;  Many 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

43、 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.</p><p>  Each stream object contains a conditio

44、n 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,

45、 much the way we used the int_quiz1 variable to represent test scores in the example in Section 5.3.1 (p. 156).</p><p>  Each IO class also defines three const values of type iostate that represent particula

46、r 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.</p><

47、p>  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, s

48、uch 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 set

49、s the failbit.</p><p>  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 i

50、n an error state. Similarly, the good operation returns TRue if none of the other conditions is true.</p><p>  The clear and setstate operations change the state of the condition member. The clear operations

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

52、indicate that a problem occurred. setstate leaves the existing state variables unchanged except that it adds the additional indicated state(s).</p><p>  Accessing the Condition State</p><p>  Th

53、e rdstate member function returns an iostate value that corresponds to the entire current condition state of the stream.</p><p>  Dealing with Multiple States</p><p>  Often we need to set or cl

54、ear 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

55、 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.</p><

56、p>  第八章 標準 IO 庫</p><p>  C++ 的輸入/輸出(input/output)由標準庫提供。標準庫定義了一族類型,支持對文件和控制窗口等設備的讀寫(IO)。還定義了其他一些類型,使 string 對象能夠像文件一樣操作,從而使我們無須 IO 就能實現(xiàn)數(shù)據(jù)與字符之間的轉換。這些 IO 類型都定義了如何讀寫內置數(shù)據(jù)類型的值。此外,一般來說,類的設計者還可以很方便地使用 IO 標準庫設施讀寫

57、自定義類的對象。類類型通常使用 IO 標準庫為內置類型定義的操作符和規(guī)則來進行讀寫。</p><p>  本章將介紹 IO標準庫的基礎知識,而更多的內容會在后續(xù)章節(jié)中介紹:第十四章考慮如何編寫自己的輸入輸出操作符:附錄 A 則介紹格式控制以及文件的隨機訪問。</p><p>  前面的程序已經(jīng)使用了多種 IO 標準庫提供的</p><p>  istream (輸入

58、流)類型,提供輸入操作。</p><p>  ostream (輸出流)類型,提供輸出操作。</p><p>  cin (發(fā)音為 see-in):讀入標準輸入的 istream 對象。</p><p>  cout (發(fā)音為 see-out):寫到標準輸出的 ostream 對象。</p><p>  cerr (發(fā)音為 see-err):

59、輸出標準錯誤的 ostream 對象。cerr 常用于程序錯誤信息。</p><p>  >> 操作符,用于從 istream 對象中讀入輸入。</p><p>  << 操作符,用于把輸出寫到 ostream 對象中。</p><p>  getline函數(shù),需要分別取 istream 類型和 string 類型的兩個引用形參,其功能是從

60、istream 對象讀取一個單詞,然后寫入 string 對象中。</p><p>  本章簡要地介紹一些附加的 IO 操作,并討論文件對象和 string 對象的讀寫。附錄 A 會介紹如何控制 IO 操作的格式、文件的隨機訪問以及無格式的 IO。本書是初級讀本,因此不會詳細討論完整的 iostream 標準庫——特別是,我們不但沒有涉及系統(tǒng)特定的實現(xiàn)細則,也不討論標準庫管理輸入輸出緩沖區(qū)的機制,以及如何編寫自定

61、義的緩沖區(qū)類。這些話題已超出了本書的范疇。相對而言,本書把重點放在 IO 標準庫對普通程序最有用的部分。</p><p>  8.1. 面向對象的標準庫</p><p>  迄今為止,我們已經(jīng)使用 IO 類型和對象讀寫數(shù)據(jù)流,它們常用于與用戶控制窗口的交互。當然,實際的程序不能僅限于對控制窗口的 IO,通常還需要讀或寫已命名的文件。此外,程序還應該能方便地使用 IO 操作格式化內存中的數(shù)據(jù)

62、,從而避免讀寫磁盤或其他設備的復雜性和運行代價。應用程序還需要支持寬字符(wide-character)語言的讀寫。</p><p>  從概念上看,無論是設備的類型還是字符的大小,都不影響需要執(zhí)行的 IO 操作。例如,不管我們是從控制窗口、磁盤文件或內存中的字符串讀入數(shù)據(jù),都可使用 >> 操作符。相似地,無論我們讀的是 char 類型的字符還是 wchar_t(第 2.1.1 節(jié))的字符,也都可以使

63、用該操作符。</p><p>  乍看起來,要同時支持或使用不同類型設備以及不同大小的字符流,其復雜程度似乎相當可怕。為了管理這樣的復雜性,標準庫使用了繼承(inheritance)來定義一組面向對象(object-oriented)類。在本書的第四部分將會更詳細地討論繼承和面向對象程序設計,不過,一般而言,通過繼承關聯(lián)起來的類型都共享共同的接口。當一個類繼承另一個類時,這兩個類通??梢允褂孟嗤牟僮?。更確切地說

64、,如果兩種類型存在繼承關系,則可以說一個類“繼承”了其父類的行為——接口。C++ 中所提及的父類稱為基類(base class),而繼承而來的類則稱為派生類(derived class)。</p><p>  IO 類型在三個獨立的頭文件中定義:iostream 定義讀寫控制窗口的類型,fstream 定義讀寫已命名文件的類型,而 sstream 所定義的類型則用于讀寫存儲在內存中的 string 對象。在 fs

65、tream 和 sstream 里定義的每種類型都是從 iostream 頭文件中定義的相關類型派生而來。表 8.1 列出了 C++ 的 IO 類,而圖 8.1 則闡明這些類型之間的繼承關系。繼承關系通??梢杂妙愃朴诩彝涞膱D解說明。最頂端的圓圈代表基類(或稱“父類”),基類和派生類(或稱“子類”)之間用線段連接。因此,圖 8.1 所示,istream 是 ifstream 和 istringstream 的基類,同時也是 iostre

66、am 的基類,而 iostream 則是 stringstream 和 fstream 的基類。</p><p>  由于 ifstream 和 istringstream 類型繼承了 istream 類,因此已知這兩種類型的大量用法。我們曾經(jīng)編寫過的讀 istream 對象的程序也可用于讀文件(使用 ifstream 類型)或者 string 對象(使用 istringstream 類型)。類似地,提供輸出功能

67、的程序同樣可用 ofstream 或 ostringstream 取代 ostream 類型實現(xiàn)。除了 istream 和 ostream 類型之外,iostream 頭文件還定義了 iostream 類型。盡管我們的程序還沒用過這種類型,但事實上可以多了解一些關于 iostream 的用法。iostream 類型由 istream 和 ostream 兩者派生而來。這意味著 iostream 對象共享了它的兩個父類的接口。也就是說,可

68、使用 iostream 類型在同一個流上實現(xiàn)輸入和輸出操作。標準庫還定義了另外兩個繼承 iostream 的類型。這些類型可用于讀寫文件或 string 對象。</p><p>  對 IO 類型使用繼承還有另外一個重要的含義:正如在第十五章可以看到的,如果函數(shù)有基類類型的引用形參時,可以給函數(shù)傳遞其派生類型的對象。這就意味著:對 istream& 進行操作的函數(shù),也可使用 ifstream 或者 ist

69、ringstream 對象來調用。類似地,形參為 ostream& 類型的函數(shù)也可用 ofstream 或者 ostringstream 對象調用。因為 IO 類型通過繼承關聯(lián),所以可以只編寫一個函數(shù),而將它應用到三種類型的流上:控制臺、磁盤文件或者字符串流(string streams)。</p><p><b>  國際字符的支持</b></p><p>

70、  迄今為止,所描述的流類(stream class)讀寫的是由 char 類型組成的流。此外,標準庫還定義了一組相關的類型,支持 wchar_t 類型。每個類都加上“w”前綴,以此與 char 類型的版本區(qū)分開來。于是,wostream、wistream 和 wiostream 類型從控制窗口讀寫 wchar_t 數(shù)據(jù)。相應的文件輸入輸出類是 wifstream、wofstream 和 wfstream。而 wchar_t 版本的 s

71、tring 輸入/輸出流則是 wistringstream、wostringstream 和 wstringstream。標準庫還定義了從標準輸入輸出讀寫寬字符的對象。這些對象加上“w”前綴,以此與 char 類型版本區(qū)分:wchar_t 類型的標準輸入對象是 wcin;標準輸出是 wcout;而標準錯誤則是 wcerr。</p><p>  每一個 IO 頭文件都定義了 char 和 wchar_t 類型的類和

72、標準輸入/輸出對象?;诹鞯?wchar_t 類型的類和對象在 iostream 中定義,寬字符文件流類型在 fstream 中定義,而寬字符 stringstream 則在 sstream 頭文件中定義。</p><p>  IO 對象不可復制或賦值</p><p>  出于某些原因,標準庫類型不允許做復制或賦值操作。其原因將在后面第三部分和第四部分學習類和繼承時闡明。</p>

73、;<p>  這個要求有兩層特別重要的含義。正如在第九章看到的,只有支持復制的元素類型可以存儲在 vector 或其他容器類型里。由于流對象不能復制,因此不能存儲在 vector(或其他)容器中(即不存在存儲流對象的 vector 或其他容器)。</p><p>  第二個含義是:形參或返回類型也不能為流類型。如果需要傳遞或返回 IO 對象,則必須傳遞或返回指向該對象的指針或引用。</p>

74、;<p>  一般情況下,如果要傳遞 IO 對象以便對它進行讀寫,可用非 const 引用的方式傳遞這個流對象。對 IO 對象的讀寫會改變它的狀態(tài),因此引用必須是非 const 的。</p><p>  8.2. Condition States</p><p>  在展開討論 fstream 和 sstream 頭文件中定義的類型之前,需要了解更多 IO 標準庫如何管理其緩

75、沖區(qū)及其流狀態(tài)的相關內容。謹記本節(jié)和下一節(jié)所介紹的內容同樣適用于普通流、文件流以及 string 流。</p><p>  實現(xiàn) IO 的繼承正是錯誤發(fā)生的根源。一些錯誤是可恢復的;一些錯誤則發(fā)生在系統(tǒng)底層,位于程序可修正的范圍之外。IO 標準庫管理一系列條件狀態(tài)(condition state)成員,用來標記給定的 IO 對象是否處于可用狀態(tài),或者碰到了哪種特定的錯誤。表 8.2 列出了標準庫定義的一組函數(shù)和標

76、記,提供訪問和操縱流狀態(tài)的手段。</p><p>  如果在標準輸入設備輸入 Borges,則 cin 在嘗試將輸入的字符串讀為 int 型數(shù)據(jù)失敗后,會生成一個錯誤狀態(tài)。類似地,如果輸入文件結束符(end-of-file),cin 也會進入錯誤狀態(tài)。而如果輸入 1024,則成功讀取,cin 將處于正確的無錯誤狀態(tài)。</p><p>  流必須處于無錯誤狀態(tài),才能用于輸入或輸出。檢測流是否

77、用的最簡單的方法是檢查其真值。</p><p>  if 語句直接檢查流的狀態(tài),而 while 語句則檢測條件表達式返回的流,從而間接地檢查了流的狀態(tài)。如果成功輸入,則條件檢測為 true。</p><p><b>  條件狀態(tài)</b></p><p>  許多程序只需知道是否有效。而某些程序則需要更詳細地訪問或控制流的狀態(tài),此時,除了知道流處

78、于錯誤狀態(tài)外,還必須了解它遇到了哪種類型的錯誤。例如,程序員也許希望弄清是到達了文件的結尾,還是遇到了 IO 設備上的錯誤。</p><p>  所有流對象都包含一個條件狀態(tài)成員,該成員由 setstate 和 clear 操作管理。這個狀態(tài)成員為 iostate 類型,這是由各個 iostream 類分別定義的機器相關的整型。該狀態(tài)成員以二進制位(bit)的形式使用,類似于第 5.3.1 節(jié)的例子中用于記錄測驗

79、成績的 int_quiz1 變量。</p><p>  每個 IO 類還定義了三個 iostate 類型的常量值,分別表示特定的位模式。這些常量值用于指出特定類型的 IO 條件,可與位操作符(第 5.3 節(jié))一起使用,以便在一次操作中檢查或設置多個標志。</p><p>  badbit 標志著系統(tǒng)級的故障,如無法恢復的讀寫錯誤。如果出現(xiàn)了這類錯誤,則該流通常就不能再繼續(xù)使用了。如果出現(xiàn)的

80、是可恢復的錯誤,如在希望獲得數(shù)值型數(shù)據(jù)時輸入了字符,此時則設置 failbit 標志,這種導致設置 failbit 的問題通常是可以修正的。eofbit 是在遇到文件結束符時設置的,此時同時還設置了 failbit。</p><p>  流的狀態(tài)由 bad、fail、eof 和 good 操作提示。如果 bad、fail 或者 eof 中的任意一個為 true,則檢查流本身將顯示該流處于錯誤狀態(tài)。類似地,如果這三

81、個條件沒有一個為 true,則 good 操作將返回 true。</p><p>  clear 和 setstate 操作用于改變條件成員的狀態(tài)。clear 操作將條件重設為有效狀態(tài)。在流的使用出現(xiàn)了問題并做出補救后,如果我們希望把流重設為有效狀態(tài),則可以調用 clear 操作。使用 setstate 操作可打開某個指定的條件,用于表示某個問題的發(fā)生。除了添加的標記狀態(tài),setstate 將保留其他已存在的狀態(tài)

82、變量不變。</p><p><b>  條件狀態(tài)的訪問</b></p><p>  rdstate 成員函數(shù)返回一個 iostate 類型值,該值對應于流當前的整個條件狀態(tài)。</p><p><b>  多種狀態(tài)的處理</b></p><p>  常常會出現(xiàn)需要設置或清除多個狀態(tài)二進制位的情況。此時

溫馨提示

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

評論

0/150

提交評論