【正文】
t 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 States Many programs need only know whether a stream is valid. Other programs need more finegrained 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 endoffile 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 machinedependent 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 (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 , 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 endoffile is encountered. Hitting endoffile 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 State The rdstate member function returns an iostate value that corresponds to the entire current condition state of the stream. Dealing with Multiple States Often 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 , 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. 第八章 標(biāo)準(zhǔn) IO 庫 C++ 的輸入/輸出( input/output)由標(biāo)準(zhǔn)庫提供。 ? ostream (輸出流)類型,提供輸出操作。 本章簡要地介紹一些附加的 IO 操作,并討論文件對象和 string 對象的讀寫。應(yīng)用程序還需要支持寬字符( widecharacter)語言的讀寫。更確切地說,如果兩種類型存在繼承關(guān)系,則可以說一個(gè)類 “ 繼承 ” 了其父類的行為 —— 接口。 由于 ifstream 和 istringstream 類型繼承了 istream 類,因此已知這兩種類型的大量用法。標(biāo)準(zhǔn)庫還定義了另外兩個(gè)繼承 iostream 的類型。 國際字符的支持 迄今為止,所描述的流類( stream class)讀寫的是由 char 類型組成的流。 每一個(gè) IO 頭文件都定義了 char 和 wchar_t 類型的類和標(biāo)準(zhǔn)輸入/輸出對象。如果需要傳遞或返回 IO 對象,則必須傳遞或返回指向該對象的指針或引用 。 表 列出了標(biāo)準(zhǔn)庫定義的一組函數(shù)和標(biāo)記,提供訪問和操縱流狀態(tài)的手段。 條件狀態(tài) 許多程序只需知道是否有效。 badbit 標(biāo)志著系統(tǒng)級