C# BinaryReader BaseStream

Description

BinaryReader BaseStream Exposes access to the underlying stream of the BinaryReader.

Syntax

BinaryReader.BaseStream has the following syntax.


public virtual Stream BaseStream { get; }

Example

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.


/*from  w w w.j av  a2 s .co  m*/
using System;
using System.IO;

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

        double[] dataArray = new double[arrayLength];
        for(i = 0; i < arrayLength; i++)
        {
            dataArray[i] = 100.1 * i;
        }

        using(BinaryWriter binWriter = new BinaryWriter(new MemoryStream()))
        {
            for(i = 0; i < arrayLength; i++)
            {
                binWriter.Write(dataArray[i]);
            }
            using(BinaryReader binReader = new BinaryReader(binWriter.BaseStream))
            {
                binReader.BaseStream.Position = 0;
                for(i = 0; i < arrayLength; i++)
                {
                    if(binReader.ReadDouble() != dataArray[i])
                    {
                        Console.WriteLine("Error writing data.");
                        break;
                    }
                }
            }
        }
    }
}




















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter