當使用標準C++編程時,我們已開始接觸到兩個主要的I/O"工具":標準C標頭檔cstdio和標準C++中與流相關的標頭檔iostream,如果加上Windows的話,那麼還有Win32庫和MFC庫,另外,還有CLI/.NET。本文將要探討的,就是C++/CLI中的輸入與輸出。
簡介
日常,我們與檔案或裝置進行通訊的邏輯通道,稱為流。資料可以8位位元組或16位Unicode字元形式進行讀寫,而兩者都有其自己的類集;另外,還有用於在位元組與字元之間轉換的類。其中,字元流通過Stream類及其的衍生類別實現;字元流通過TextReader與TextWriter類及其的衍生類別實現。
在圖1中示範了標準I/O的類繼承關係。(帶有System命名空間首碼的類與I/O無關,但其卻是I/O類的基類。)
圖1:標準I/O類繼承關係
System::Object
System::Attribute
System::ComponentModel::MemberAttribute
System::ComponentModel::DescriptionAttribute
IODescriptionAttribute
System::ComponentModel::Component
FileSystemWatcher
System::Delegate
FileSystemEventHandler
RenamedEventHandler
System::EventArgs
FileSystemEventArgs
RenamedEventArgs
System::Exception
IOException
DirectoryNotFoundException
EndOfStreamException
FileNotFoundException
PathTooLongException
System::SystemException
InternalBufferOverflowException
BinaryReader
BinaryWriter
FileSystemEntry
Directory
File
Stream
BufferedStream
FileStream
MemoryStream
TextReader
StreamReader
StringReader
TextWriter
StreamWriter
StringWriter
System::ValueType
System::Enum
ChangedFilters
FileAccess
FileMode
FileShare
FileSystemAttributes
SeekOrigin
WatcherChangeTypes
WatcherTarget
WaitForChangedResult
每當一個程式運行時,會自動為我們開啟三個流,分別是: