Windows Dev Center

Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

BinaryReader.ReadSingle Method

Reads a 4-byte floating point value from the current stream and advances the current position of the stream by four bytes.

[Visual Basic]
Public Overridable Function ReadSingle() As Single
[C#]
public virtual float ReadSingle();
[C++]
public: virtual float ReadSingle();
[JScript]
public function ReadSingle() : float;

Return Value

A 4-byte floating point value read from the current stream.

Exceptions

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

Remarks

BinaryReader stores this data type in little endian format.

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++] This code example is part of a larger example provided for the BinaryReader class.

[Visual Basic] 
Dim binReader As New BinaryReader( _
    File.Open(fileName, FileMode.Open))
Try

    ' If the file is not empty, 
    ' read the application settings.
    If binReader.PeekChar() <> -1 Then
        aspRatio   = binReader.ReadSingle()
        lkupDir     = binReader.ReadString()
        saveTime  = binReader.ReadInt32()
        statusBar = binReader.ReadBoolean()
        Return
    End If

' 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 ex As EndOfStreamException
    Console.WriteLine("{0} caught and ignored. " & _ 
        "Using default values.", ex.GetType().Name)
Finally
    binReader.Close()
End Try

[C#] 
BinaryReader binReader = 
    new BinaryReader(File.Open(fileName, FileMode.Open));
try
{
    // If the file is not empty, 
    // read the application settings.
    if(binReader.PeekChar() != -1)
    {
        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();
}

[C++] 
BinaryReader* binReader = 
    new BinaryReader(File::Open(fileName, FileMode::Open));
try
{
    // If the file is not empty, 
    // read the application settings.
    if(binReader->PeekChar() != -1)
    {
        aspectRatio   = binReader->ReadSingle();
        lookupDir     = binReader->ReadString();
        autoSaveTime  = binReader->ReadInt32();
        showStatusBar = binReader->ReadBoolean();
        return;
    }
}

// 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(S"{0} caught and ignored. "
        S"Using default values.", e->GetType()->Name);
}
__finally
{
    binReader->Close();
}

[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 | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2015 Microsoft