BinaryReader.ReadChar Method
Reads the next character from the current stream and advances the current position of the stream in accordance with the Encoding used and the specific character being read from the stream.
[Visual Basic] Public Overridable Function ReadChar() As Char [C#] public virtual char ReadChar(); [C++] public: virtual __wchar_t ReadChar(); [JScript] public function ReadChar() : Char;
Return Value
A character read from the current stream.
Exceptions
| Exception Type | Condition |
|---|---|
| EndOfStreamException | The end of the stream is reached. |
| ObjectDisposedException | The stream is closed. |
| IOException | An I/O error occurs. |
Remarks
The following table lists examples of other typical or related I/O tasks.
| To do this... | See the example in this topic... |
|---|---|
| Create a text file. | Writing Text to a File |
| Write to a text file. | Writing Text to a File |
| Read from a text file. | Reading Text from a File |
| Append text to a file. | Opening and Appending to a Log File |
| Get the size of a file. | FileInfo.Length |
| Get the attributes of a file. | File.GetAttributes |
| Set the attributes of a file. | File.SetAttributes |
| Determine if a file exists. | File.Exists |
| Read from a binary file. | Reading and Writing to a Newly Created Data File |
| Write to a binary file. | Reading and Writing to a Newly Created Data File |
Example
[Visual Basic, C#, C++] The following code example shows how to read and write data using memory as a backing store.
[Visual Basic] Imports System Imports System.IO Public Class BinaryRW Shared Sub Main() Dim i As Integer = 0 Dim invalidPathChars() As Char = Path.InvalidPathChars Dim memStream As new MemoryStream() Dim binWriter As New BinaryWriter(memStream) ' Write to memory. binWriter.Write("Invalid file path characters are: ") For i = 0 To invalidPathChars.Length - 1 binWriter.Write(invalidPathChars(i)) Next i ' Create the reader using the same MemoryStream ' as used with the writer. Dim binReader As New BinaryReader(memStream) ' Set Position to the beginning of the stream. memStream.Position = 0 ' Read the data from memory and write it to the console. Console.Write(binReader.ReadString()) Dim memoryData( _ CInt(memStream.Length - memStream.Position) - 1) As Char For i = 0 To memoryData.Length - 1 memoryData(i) = binReader.ReadChar() Next i Console.WriteLine(memoryData) End Sub End Class [C#] using System; using System.IO; class BinaryRW { static void Main() { int i = 0; char[] invalidPathChars = Path.InvalidPathChars; MemoryStream memStream = new MemoryStream(); BinaryWriter binWriter = new BinaryWriter(memStream); // Write to memory. binWriter.Write("Invalid file path characters are: "); for(i = 0; i < invalidPathChars.Length; i++) { binWriter.Write(invalidPathChars[i]); } // Create the reader using the same MemoryStream // as used with the writer. BinaryReader binReader = new BinaryReader(memStream); // Set Position to the beginning of the stream. memStream.Position = 0; // Read the data from memory and write it to the console. Console.Write(binReader.ReadString()); char[] memoryData = new char[memStream.Length - memStream.Position]; for(i = 0; i < memoryData.Length; i++) { memoryData[i] = binReader.ReadChar(); } Console.WriteLine(memoryData); } } [C++] #using <mscorlib.dll> using namespace System; using namespace System::IO; void main() { int i; Char invalidPathChars __gc[] = Path::InvalidPathChars; MemoryStream* memStream = new MemoryStream(); BinaryWriter* binWriter = new BinaryWriter(memStream); // Write to memory. binWriter->Write(S"Invalid file path characters are: "); for(i = 0; i < invalidPathChars->Length; i++) { binWriter->Write(invalidPathChars[i]); } // Create the reader using the same MemoryStream // as used with the writer. BinaryReader* binReader = new BinaryReader(memStream); // Set Position to the beginning of the stream. binReader->BaseStream->Position = 0; // Read the data from memory and write it to the console. Console::Write(binReader->ReadString()); Char memoryData __gc[] = new Char __gc[memStream->Length - memStream->Position]; for(i = 0; i < memoryData->Length; i++) { memoryData[i] = binReader->ReadChar(); } Console::WriteLine(memoryData); }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework
See Also
BinaryReader Class | BinaryReader Members | System.IO Namespace | Encoding | Working with I/O | Reading Text from a File | Writing Text to a File