Export (0) Print
Expand All

BinaryReader.ReadChars Method

Reads count characters from the current stream, returns the data in a character array, and advances the current position in accordance with the Encoding used and the specific character being read from the stream.

[Visual Basic]
Public Overridable Function ReadChars( _
   ByVal count As Integer _
) As Char()
[C#]
public virtual char[] ReadChars(
 int count
);
[C++]
public: virtual __wchar_t ReadChars(
 int count
)  __gc[];
[JScript]
public function ReadChars(
   count : int
) : Char[];

Parameters

count
The number of characters to read.

Return Value

A character array containing data read from the underlying stream. This might be less than the number of characters requested if the end of the stream is reached.

Exceptions

Exception Type Condition
EndOfStreamException The end of the stream is reached.
ObjectDisposedException The stream is closed.
IOException An I/O error occurs.
ArgumentOutOfRangeException count is negative.

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

File.AppendText

FileInfo.AppendText

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 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: ")
        binWriter.Write(Path.InvalidPathChars)

        ' 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())
        Console.WriteLine(binReader.ReadChars( _
            CInt(memStream.Length - memStream.Position)))
    
    End Sub
End Class

[C#] 
using System;
using System.IO;

class BinaryRW
{
    static void Main()
    {
        char[] invalidPathChars = Path.InvalidPathChars;
        MemoryStream memStream = new MemoryStream();
        BinaryWriter binWriter = new BinaryWriter(memStream);

        // Write to memory.
        binWriter.Write("Invalid file path characters are: ");
        binWriter.Write(Path.InvalidPathChars);

        // 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());
        Console.WriteLine(binReader.ReadChars(
            (int)(memStream.Length - memStream.Position)));
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;

void main()
{
    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: ");
    binWriter->Write(Path::InvalidPathChars);

    // 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());
    Console::WriteLine(binReader->ReadChars(
        (int)(memStream->Length - memStream->Position)));
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter 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

Show:
© 2014 Microsoft