次の方法で共有


BinaryReader.ReadDouble メソッド

現在のストリームから 8 バイト浮動小数点値を読み取り、ストリームの現在位置を 8 バイトだけ進めます。

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

戻り値

現在のストリームから読み取った 8 バイト浮動小数点値。

例外

例外の種類 条件
EndOfStreamException ストリームの末尾に到達しました。
ObjectDisposedException ストリームが閉じられました。
IOException I/O エラーが発生しました。

解説

BinaryReader は、このデータ型をリトル エンディアン形式で格納します。

その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。

実行するタスク 参考例があるトピック
テキスト ファイルを作成する。 ファイルへのテキストの書き込み
テキスト ファイルに書き込む。 ファイルへのテキストの書き込み
テキスト ファイルから読み取る。 ファイルからのテキストの読み取り
テキストをファイルに追加する。 ログ ファイルのオープンと追加

File.AppendText

FileInfo.AppendText

ファイルのサイズを取得する。 FileInfo.Length
ファイルの属性を取得する。 File.GetAttributes
ファイルの属性を設定する。 File.SetAttributes
ファイルが存在するかどうかを判別する。 File.Exists
バイナリ ファイルから読み取る。 新しく作成したデータ ファイルの読み取りと書き込み
バイナリ ファイルに書き込む。 新しく作成したデータ ファイルの読み取りと書き込み

使用例

[Visual Basic, C#, C++] 次に示すのは、 MemoryStream クラス上で BinaryReader クラスと BinaryWriter クラスを使用し、メモリに対して Double データの読み取りと書き込みを実行するコード例です。 MemoryStreamByte データだけを読み書きします。

 
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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

BinaryReader クラス | BinaryReader メンバ | System.IO 名前空間 | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み