BinaryReader.ReadChars(Int32) Méthode

Définition

Lit le nombre spécifié de caractères du flux actuel, retourne les données d’un tableau de caractères et avance la position actuelle conformément à l’élément Encoding utilisé et au caractère spécifique lu à partir du flux.

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()

Paramètres

count
Int32

Nombre de caractères à lire.

Retours

Char[]

Tableau de caractères contenant des données lues à partir du flux sous-jacent. Cela peut être inférieur au nombre de caractères demandé si la fin du flux est atteinte.

Exceptions

Le nombre de caractères décodés à lire est supérieur à count. Cela peut se produire si un décodeur Unicode retourne des caractères de secours ou une paire de substitution.

Le flux est fermé.

Une erreur d’E/S s’est produite.

count est négatif.

Exemples

L’exemple de code suivant montre comment lire et écrire des données à l’aide de la mémoire en tant que magasin de stockage.

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

Remarques

BinaryReader ne restaure pas la position du fichier après une opération de lecture infructueuse.

Lors de la lecture à partir de flux réseau, dans certains cas rares, la ReadChars méthode peut lire un caractère supplémentaire à partir du flux si celle-ci a été construite avec l’encodage BinaryReader Unicode. Si cela se produit, vous pouvez utiliser la ReadBytes méthode pour lire un tableau d’octets de longueur fixe, puis passer ce tableau à la ReadChars méthode.

S’applique à

Voir aussi