C++/istream

維基教科書,自由的教學讀本
< C++

istreamw:C++標準程式庫中的一個w:頭文件,定義了C++標準中的文件流輸入的幾個模板類與類型。

類模板[編輯]

下述聲明中,g為get的縮寫,p為put的縮寫。

  • basic_istream: 輸入流
    • 運算符函數
      • operator>> 從流中讀取指定類型的數據。注意,空白符被忽略。
    • 成員函數:
      • gcount 返回上一次無格式化讀取中的字符數量
      • get 4個重載版本。讀取1個字符,包含空白符;或者讀取多個字符到緩衝區,直至回車符、EOF或指定截止字符(均不含)。截止符保留在流中。
      • getline 讀取一行字符或至指定截止字符(均不含)。截止符捨棄。
      • ignore 從當前位置忽略掉指定個數的字符
      • peek 查看下一個要讀出的字符
      • putback 把一個字符放回到輸入流中。會在讀取操作發生之前檢查,參數 istream &​ c是否是最後一個讀入的字符。
      • read 無格式地嚴格讀取n個字符,遇EOF拋出異常
      • readsome 不阻塞地讀取最多n個字符,或至EOF(不拋出異常)
      • seekg 在流中移動讀指針的位置,可以絕對尋址或相對尋址。
      • sentry 成員類,做一些異常安全的前置和後置工作。 所有輸入操作的成員函數在執行前都會構造一個sentry對象.[1]如果該對象構造成功,會對該對象求bool值。僅當bool值為真,輸入操作函數會繼續執行;否則函數返回不執行輸入操作。如果該對象構造失敗,格式化函數會根據情況設置badbit位,並拋出異常。成員函數返回之前,會摧毀這個sentry對象。
      • swap 交換basic_istream對象
      • sync 將緩衝區的內容與關聯的字符流同步。內部調用順序是istream::sync -> basic_streambuf::pubsync -> basic_streambuf::sync。如果是標準輸入流,則是將緩衝區的內容清空,保持與鍵盤輸入的同步; 如果是文件流,則是將緩衝區內容與文件內容同步。
      • tellg 獲得流輸入位置,基於0.
      • unget 把最近讀入的字符放回流中
  • basic_iostream: 輸入/輸出流。多繼承了basic_istream和basic_ostream,並定義了幾個protected的移動賦值運算符和swap成員函數。

[編輯]

  • istream: 輸入流(類)
  • iostream: 輸入/輸出流(類)
  • wistream: 輸入流(寬字符) (類)
  • wiostream: 輸入/輸出流(寬字符) (類)

函數模板[編輯]

  • ws: 抽取空白符

類型定義[編輯]

  • typedef basic_istream<char> istream
  • typedef basic_istream<wchar_t> wistream
  • typedef basic_iostream<char> iostream
  • typedef basic_iostream<wchar_t> wiostream

參考文獻[編輯]

  1. sentry對象的構造函數會執行: If any of its internal error flags is set, the function sets its failbit flag and returns. If it is a tied stream, the function flushes the stream it is tied to (if its output buffer is not empty). The class may implement ways for library functions to defer this flush until the next call to overflow by its associated stream buffer. If its skipws format flag is set, and the constructor is not passed true as second argument (noskipws), all leading whitespace characters (locale-specific) are extracted and discarded. If this operation exhausts the source of characters, the function sets both the failbit and eofbit internal state flags