System.IO.BinaryReader.ReadSingle 方法

方法描述

从当前流中读取 4 字节浮点值,并使流的当前位置提升 4 个字节。

语法定义(C# System.IO.BinaryReader.ReadSingle 方法 的用法)

public virtual float ReadSingle()

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
返回值 System.Single 从当前流中读取的 4 字节浮点值。

提示和注释

BinaryReader 在读取失败后不还原文件位置。

BinaryReader 以 Little-Endian 格式读取此数据类型。

有关通用 I/O 任务的列表,请参见通用 I/O 任务。

System.IO.BinaryReader.ReadSingle 方法例子

此代码示例摘自为 BinaryReader 类提供的一个更大的示例。

BinaryReader binReader =
    new BinaryReader(File.Open(fileName, FileMode.Open));
try
{
    // If the file is not empty,
    // read the application settings.
    // First read 4 bytes into a buffer to
    // determine if the file is empty.
    byte[] testArray = new byte[3];
    int count = binReader.Read(testArray, 0, 3);

    if (count != 0)
    {
        // Reset the position in the stream to zero.
        binReader.BaseStream.Seek(0, SeekOrigin.Begin);

        aspectRatio   = binReader.ReadSingle();
        lookupDir     = binReader.ReadString();
        autoSaveTime  = binReader.ReadInt32();
        showStatusBar = binReader.ReadBoolean();
    }
}

// If the end of the stream is reached before reading
// the four data values, ignore the error and use the
// default settings for the remaining values.
catch(EndOfStreamException e)
{
    Console.WriteLine("{0} caught and ignored. " +
        "Using default values.", e.GetType().Name);
}
finally
{
    binReader.Close();
}

异常

异常 异常描述
EndOfStreamException 已到达流的末尾。
ObjectDisposedException 流已关闭。
IOException 发生 I/O 错误。

命名空间

namespace: System.IO

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。