BinaryReader.ReadChars(Int32) Metoda

Definice

Přečte zadaný počet znaků z aktuálního datového proudu, vrátí data v matici znaků a posune aktuální pozici v souladu s použitým Encoding a konkrétním znakem, který se čte z datového proudu.

public:
 virtual cli::array <char> ^ ReadChars(int count);
public virtual char[] ReadChars(int count);
abstract member ReadChars : int -> char[]
override this.ReadChars : int -> char[]
Public Overridable Function ReadChars (count As Integer) As Char()

Parametry

count
Int32

Počet znaků, které se mají přečíst.

Návraty

Char[]

Pole znaků obsahující data načtená z podkladového datového proudu. Pokud dosáhnete konce datového proudu, může to být menší než požadovaný počet znaků.

Výjimky

Počet dekódovaných znaků pro čtení je větší než count. K tomu může dojít v případě, že dekodér Unicode vrátí náhradní znaky nebo náhradní dvojici.

Stream je zavřený.

Došlo k vstupně-výstupní chybě.

count je negativní.

Příklady

Následující příklad kódu ukazuje, jak číst a zapisovat data pomocí paměti jako záložní úložiště.

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)));
    }
}
open System.IO

let invalidPathChars = Path.GetInvalidPathChars()
let memStream = new MemoryStream()
let binWriter = new BinaryWriter(memStream)

// Write to memory.
binWriter.Write "Invalid file path characters are: "
binWriter.Write invalidPathChars

// Create the reader using the same MemoryStream
// as used with the writer.
let 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.
printf $"{binReader.ReadString()}"
printfn $"{binReader.ReadChars(int (memStream.Length - memStream.Position))}"
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

Poznámky

BinaryReader neobnoví pozici souboru po neúspěšné operaci čtení.

Při čtení ze síťových datových proudů může metoda v některých výjimečných případech číst další znak z datového proudu, ReadChars pokud BinaryReader byl vytvořen s kódováním Unicode. Pokud k tomu dojde, můžete použít metodu ReadBytes ke čtení pole bajtů s pevnou délkou a pak předat toto pole metodě ReadChars .

Platí pro

Viz také