ReadDouble Method

BinaryReader.ReadDouble Method

Reads an 8-byte floating point value from the current stream and advances the current position of the stream by eight bytes.

[Visual Basic]
Public Overridable Function ReadDouble() As Double
[C#]
public virtual double ReadDouble();
[C++]
public: virtual double ReadDouble();
[JScript]
public function ReadDouble() : double;

Return Value

An 8-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++] The following code example shows how to read and write Double data to memory by using the BinaryReader and BinaryWriter classes on top of the MemoryStream class. MemoryStream only reads and writes Byte data.

[Visual Basic] 
Imports System
Imports System.IO

Public Class BinaryRW

    Shared Sub Main()
    
        Dim i As Integer
        Const upperBound As Integer = 1000

        ' Create random data to write to the stream.
        Dim dataArray(upperBound) As Double
        Dim randomGenerator As New Random()
        For i = 0 To upperBound
            dataArray(i) = 100.1 * randomGenerator.NextDouble()
        Next i

        Dim binWriter As New BinaryWriter(New MemoryStream())
        Try

            ' Write data to the stream.
            Console.WriteLine("Writing data to the stream.")
            
            For i = 0 To upperBound
                binWriter.Write(dataArray(i))
            Next i

            ' Create a reader using the stream from the writer.
            Dim binReader As New BinaryReader(binWriter.BaseStream)

            ' Return to the beginning of the stream.
            binReader.BaseStream.Position = 0

            ' Read and verify the data.
            Try
                Console.WriteLine("Verifying the written data.")
                For i = 0 To upperBound
                    If binReader.ReadDouble() <> dataArray(i) Then
                        Console.WriteLine("Error writing data.")
                        Exit For
                    End If
                Next i
                Console.WriteLine("The data was written and verified.")
            Catch ex As EndOfStreamException
                Console.WriteLine("Error writing data: {0}.", _
                    ex.GetType().Name)
            End Try
        Finally
            binWriter.Close()
        End Try

    End Sub
End Class

[C#] 
using System;
using System.IO;

class BinaryRW
{
    static void Main()
    {
        int i;
        const int arrayLength = 1000;

        // Create random data to write to the stream.
        Random randomGenerator = new Random();
        double[] dataArray = new double[arrayLength];
        for(i = 0; i < arrayLength; i++)
        {
            dataArray[i] = 100.1 * randomGenerator.NextDouble();
        }

        using(BinaryWriter binWriter = 
            new BinaryWriter(new MemoryStream()))
        {
            // Write the data to the stream.
            Console.WriteLine("Writing data to the stream.");
            for(i = 0; i < arrayLength; i++)
            {
                binWriter.Write(dataArray[i]);
            }

            // Create a reader using the stream from the writer.
            using(BinaryReader binReader = 
                new BinaryReader(binWriter.BaseStream))
            {
                try
                {
                    // Return to the beginning of the stream.
                    binReader.BaseStream.Position = 0;

                    // Read and verify the data.
                    Console.WriteLine("Verifying the written data.");
                    for(i = 0; i < arrayLength; i++)
                    {
                        if(binReader.ReadDouble() != dataArray[i])
                        {
                            Console.WriteLine("Error writing data.");
                            break;
                        }
                    }
                    Console.WriteLine("The data was written " +
                        "and verified.");
                }
                catch(EndOfStreamException e)
                {
                    Console.WriteLine("Error writing data: {0}.",
                        e.GetType().Name);
                }
            }
        }
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;

void main()
{
    int i;
    const int arrayLength = 1000;

    // Create random data to write to the stream.
    double dataArray __gc[] = new double __gc[arrayLength];
    Random* randomGenerator = new Random();
    for(i = 0; i < arrayLength; i++)
    {
        dataArray[i] = 100.1 * randomGenerator->NextDouble();
    }

    BinaryWriter* binWriter  = 
        new BinaryWriter(new MemoryStream());
    try
    {
        // Write data to the stream.
        Console::WriteLine(S"Writing data to the stream.");
        i = 0;
        for(i = 0; i < arrayLength; i++)
        {
                binWriter->Write(dataArray[i]);
        }

        // Create a reader using the stream from the writer.
        BinaryReader* binReader  = 
            new BinaryReader(binWriter->BaseStream);

        // Return to the beginning of the stream.
        binReader->BaseStream->Position = 0;

        try
        {
            // Read and verify the data.
            i = 0;
            Console::WriteLine(S"Verifying the written data.");
            for(i = 0; i < arrayLength; i++)
            {
                if(binReader->ReadDouble() != dataArray[i])
                {
                    Console::WriteLine(S"Error writing data.");
                    break;
                }
            }
            Console::WriteLine(S"The data was written and verified.");
        }
        catch(EndOfStreamException* e)
        {
            Console::WriteLine(S"Error writing data: {0}.",
                e->GetType()->Name);
        }
    }
    __finally
    {
        binWriter->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:
© 2016 Microsoft