BinaryReader.ReadSingle BinaryReader.ReadSingle BinaryReader.ReadSingle BinaryReader.ReadSingle Method

Definition

Liest einen 4-Byte-Gleitkommawert aus dem aktuellen Stream und erhöht die aktuelle Position im Stream um 4 Bytes.Reads a 4-byte floating point value from the current stream and advances the current position of the stream by four bytes.

public:
 virtual float ReadSingle();
public virtual float ReadSingle ();
abstract member ReadSingle : unit -> single
override this.ReadSingle : unit -> single
Public Overridable Function ReadSingle () As Single

Gibt zurück

Ein aus dem aktuellen Stream gelesener 4-Byte-Gleitkommawert.A 4-byte floating point value read from the current stream.

Ausnahmen

Das Ende des Streams ist erreicht.The end of the stream is reached.

E/A-FehlerAn I/O error occurred.

Beispiele

Im folgenden Codebeispiel wird veranschaulicht, wie Anwendungseinstellungen in einer Datei gespeichert und abgerufen werden.The following code example demonstrates how to store and retrieve application settings in a file.

using System;
using System.IO;

class ConsoleApplication
{
    const string fileName = "AppSettings.dat";

    static void Main()
    {
        WriteDefaultValues();
        DisplayValues();
    }

    public static void WriteDefaultValues()
    {
        using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
        {
            writer.Write(1.250F);
            writer.Write(@"c:\Temp");
            writer.Write(10);
            writer.Write(true);
        }
    }

    public static void DisplayValues()
    {
        float aspectRatio;
        string tempDirectory;
        int autoSaveTime;
        bool showStatusBar;

        if (File.Exists(fileName))
        {
            using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
            {
                aspectRatio = reader.ReadSingle();
                tempDirectory = reader.ReadString();
                autoSaveTime = reader.ReadInt32();
                showStatusBar = reader.ReadBoolean();
            }

            Console.WriteLine("Aspect ratio set to: " + aspectRatio);
            Console.WriteLine("Temp directory is: " + tempDirectory);
            Console.WriteLine("Auto save time set to: " + autoSaveTime);
            Console.WriteLine("Show status bar: " + showStatusBar);
        }
    }
}
Imports System.IO

Module Module1
    Const fileName As String = "AppSettings.dat"

    Sub Main()
        WriteDefaultValues()
        DisplayValues()
    End Sub

    Sub WriteDefaultValues()
        Using writer As BinaryWriter = New BinaryWriter(File.Open(fileName, FileMode.Create))
            writer.Write(1.25F)
            writer.Write("c:\Temp")
            writer.Write(10)
            writer.Write(True)
        End Using
    End Sub

    Sub DisplayValues()
        Dim aspectRatio As Single
        Dim tempDirectory As String
        Dim autoSaveTime As Integer
        Dim showStatusBar As Boolean

        If (File.Exists(fileName)) Then

            Using reader As BinaryReader = New BinaryReader(File.Open(fileName, FileMode.Open))
                aspectRatio = reader.ReadSingle()
                tempDirectory = reader.ReadString()
                autoSaveTime = reader.ReadInt32()
                showStatusBar = reader.ReadBoolean()
            End Using

            Console.WriteLine("Aspect ratio set to: " & aspectRatio)
            Console.WriteLine("Temp directory is: " & tempDirectory)
            Console.WriteLine("Auto save time set to: " & autoSaveTime)
            Console.WriteLine("Show status bar: " & showStatusBar)
        End If
    End Sub

End Module

Hinweise

BinaryReader stellt die Dateiposition nach einem erfolglosen Lesevorgang nicht wieder her.BinaryReader does not restore the file position after an unsuccessful read.

BinaryReader liest diesen Datentyp im Little-Endian-Format.BinaryReader reads this data type in little-endian format.

Eine Liste der allgemeinen e/a-Aufgaben finden Sie unter Allgemeine e/a-Aufgaben.For a list of common I/O tasks, see Common I/O Tasks.

Gilt für:

Siehe auch