【正文】
的故障,如無法恢復的讀寫錯誤。 clear 操作將條件重設為有效狀態(tài)。按位或操作使用其操作數(shù)的二進制位模式產生一個整型數(shù)值。 條件狀態(tài)的訪問 rdstate 成員函數(shù)返回一個 iostate 類型值,該值對應于流當前的整個條件狀態(tài) 。 流的狀態(tài)由 bad、 fail、 eof 和 good 操作提示。這個狀態(tài)成員為 iostate 類型,這是由各個 iostream 類分別定義的機器相關的整型。 流必須處于無錯誤狀 態(tài),才能用于輸入或輸出。謹記本節(jié)和下一節(jié)所介紹的內容同樣適用于普通流、文件流以及 string 流。 這個要求有兩層特別重要的含義。相應的文件輸入輸出類是 wifstream、 wofstream 和 wfstream。 進行操作的函數(shù),也可使用 ifstream 或者 istringstream 對象來調用。盡管我們的程序還沒用過這種類型,但事實上可以多了解一些關于 iostream 的用法。 表 列出了 C++ 的 IO 類,而 圖 則闡明這些類型之間的繼承關系。 乍看起來,要同時支持或使用不同類型設備以及不同大小的字符流,其復雜程度似乎相當可怕。相對而言,本書把重點放在 IO 標準庫對普通程序最有用的部分。 cerr 常用于程序錯誤信息。此外,一般來說,類的 設計者還可以很方便地使用 IO 標準庫設施讀寫自定義類的對象。 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 Support The stream classes described thus far read and write streams posed 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。 2020 年畢業(yè)設計論文英漢互譯部分 Chapter 8. The IO Library In 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 builtin 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 builtin 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。 standard output is wcout。類類型通常使用 IO 標準庫為內置類型定義的操作符和規(guī)則來進行讀寫。 ? 操作符,用于從 istream 對象中讀入 輸入。 . 面向對象的標準庫 迄今為止,我們已經使用 IO 類型和對象讀寫數(shù)據(jù)流,它們常用于與用戶控制窗口的交互。為了管理這樣的復雜性,標準庫使用了 繼承( inheritance)來定義一組 面向對象( objectoriented) 類。繼承關系通??梢杂妙愃朴诩彝涞膱D解說明。 iostream 類型由 istream 和 ostream 兩者派生而來。類似地,形參為 ostreamamp。而 wchar_t 版本的 string 輸入/輸出流則是 wistringstream、 wostringstream 和 wstringstream。正如在 第九章 看到的,只有支持復制的元素類型可以存儲在 vector 或其他容器類型里。 實現(xiàn) IO 的繼承正是錯誤發(fā)生的根源。檢測流是否用的最簡單的方法是檢查其真值 。該狀態(tài) 成員以二進制位( bit)的形式使用,類似于 第 節(jié)的例子中用于記錄測驗成績的 int_quiz1 變量。如果 bad、 fail 或者 eof 中的任意一個為 true,則檢查流本身將顯示該流處于錯誤狀態(tài)。 多種狀態(tài)的處理 常常會出現(xiàn)需要設置或清除多個狀態(tài)二進制位的情況。另外一種方法則是使用按位或( OR)操作符( 第 節(jié) )在一次調用中生成 “ 傳遞兩個或更多狀態(tài)位 ” 的值。 clear 和 setstate 操作用于改變條件成員的狀態(tài)。這些常量值用于指出特定類型的 IO 條件,可與位操作符 ( 第 節(jié) )一起使用,以便在一次操作中檢查或設置多個標志。如果成功輸入,則條件檢測為 true。 IO 標準庫管理一系列 條件狀態(tài)( condition state) 成員,用來標記給定的 IO 對象是否處于可用狀態(tài),或者碰到了哪種特定的錯誤。 第二個含義是:形參或返回類型也不能為流類型。這些對象加上 “ w” 前綴,以此與 char 類型版本區(qū)分: wchar_t 類型的標準輸入對象是 wcin;標準輸出是 wcout;而標準錯誤則是 wcerr。因為 IO 類型通過繼承關聯(lián),所以可以只編寫一個函數(shù),而將它應用到三種類型的流上:控制臺、磁盤文件或者 字符串流( string streams) 。也就是說,可使用 iostream 類型在同一個流上實現(xiàn)輸入和輸出操作。因此, 圖 所示, istream 是 ifstream 和 istringstre