Home  |  Delphi .net Home  |  System.IO.BinaryReader  |  Read Method
Read  
Method  
Reads the next character or block of characters from the current stream
BinaryReader Class
System.IO NameSpace
CF1.  Function Read ( ) : Integer ;
CF2.  Function Read ( Bytes:Array of ByteBytes : Array of Byte; StartIndex : Integer; Count : Integer; ) : Integer ;
CF3.  Function Read ( Characters:Array of CharCharacters : Array of Char; StartIndex : Integer; Count : Integer; ) : Integer;
CF : Methods with this mark are Compact Framework Compatible
Description
If the current stream has not been exhausted, the next character, or block of characters/bytes are returned.
 
The stream position is updated accordingly.
 
In the first syntax, the Read method returns the next stream character is returned as an integer. The integer value is the numeric equivalent of the character, or -1 if the string was exhausted.
 
In the second and third syntaxes, an attempt is made to read up to Count bytes/characters. These are overlaid onto the Bytes or Characters array as appropriate from StartIndex. The returned integer value is the number of bytes/characters overlaid. Zero if the string was already exhausted before the read.
Notes
You must set the length of the target Byte/Character array in the second syntax, and it must have sufficient capacity to hold the returned bytes/characters.
Microsoft MSDN Links
System.IO
System.IO.BinaryReader
 
 
Reading a single character at a time
// Full Unit code.
// -------------------------------------------------------------
// Create a new WinForm application, double click the form to
// create an OnLoad event, and then replace the WinForm unit
// with this text.
 
unit WinForm;
 
interface
 
uses
  System.Drawing, System.Collections, System.ComponentModel,
System.Windows.Forms, System.Data;
 
type
  TWinForm = class(System.Windows.Forms.Form)
  \{REGION 'Designer Managed Code'\} // Note that REGION and ENREGION should be prefixed by a dollar sign
  strict private
    ///
    /// Required designer variable.
    ///

    Components: System.ComponentModel.Container;
    ///
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    ///

    procedure InitializeComponent;
    procedure TWinForm_Load(sender: System.Object; e: System.EventArgs);
  {ENDREGION}
  strict protected
    ///
    /// Clean up any resources being used.
    ///

    procedure Dispose(Disposing: Boolean); override;
  private
    { Private Declarations }
  public
    constructor Create;
  end;
 
  [assembly: RuntimeRequiredAttribute(TypeOf(TWinForm))]
 
implementation
 
\{REGION 'Windows Form Designer generated code'\}
///
/// Required method for Designer support -- do not modify
/// the contents of this method with the code editor.
///

 
 
procedure TWinForm.InitializeComponent;
begin
  //
  // TWinForm
  //
  Self.AutoScaleBaseSize := System.Drawing.Size.Create(5, 13);
  Self.ClientSize := System.Drawing.Size.Create(292, 266);
  Self.Name := 'TWinForm';
  Self.Text := 'WinForm';
  Include(Self.Load, Self.TWinForm_Load);
end;
{ENDREGION}
 
procedure TWinForm.Dispose(Disposing: Boolean);
begin
  if Disposing then
  begin
    if Components <> nil then
      Components.Dispose();
  end;
  inherited Dispose(Disposing);
end;
 
constructor TWinForm.Create;
begin
  inherited Create;
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent;
  //
  // TODO: Add any constructor code after InitializeComponent call
  //
end;
 
procedure TWinForm.TWinForm_Load(sender: System.Object; e: System.EventArgs);
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

var
  MyFileStream  : System.IO.FileStream;
  MyFileWriter  : System.IO.BinaryWriter;
  MyFileReader  : System.IO.BinaryReader;

begin
  // Create and open our binary file as a stream
  MyFileStream := System.IO.File.Open('C:DelphiBasics.txt',
                                      System.IO.FileMode.Create);

  // Create a BinaryWriter to allow writing to this file
  MyFileWriter := System.IO.BinaryWriter.Create(MyFileStream);

  // Write to the file
  MyFileWriter.Write('A');
  MyFileWriter.Write('B');
  MyFileWriter.Write('C');

  // Close the writer and the stream
  MyFileWriter.Close;
  MyFileStream.Close;

  // Reopen the stream for reading
  MyFileStream := System.IO.File.Open('C:DelphiBasics.txt',
                                      System.IO.FileMode.Open);

  // Create a BinaryReader to allow the file to be read back
  MyFileReader := System.IO.BinaryReader.Create(MyFileStream);

  // Display the file contents
  while MyFileReader.PeekChar <> -1 do
    Console.WriteLine(Char(MyFileReader.Read));

  // Close the reader and the stream
  MyFileReader.Close;
  MyFileStream.Close;

  Console.Readline;
end.
 
end.
Hide full unit code
  A
  B
  C
Reading blocks of characters
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

var
  MyFileStream  : System.IO.FileStream;
  MyFileWriter  : System.IO.BinaryWriter;
  MyFileReader  : System.IO.BinaryReader;
  MyCharArray   : Array of Char;
  Index         : Integer;

begin
  // Create and open our binary file as a stream
  MyFileStream := System.IO.File.Open('C:DelphiBasics.txt',
                                      System.IO.FileMode.Create);

  // Create a BinaryWriter to allow writing to this file
  MyFileWriter := System.IO.BinaryWriter.Create(MyFileStream);

  // Write to the file
  MyFileWriter.Write('A');
  MyFileWriter.Write('B');
  MyFileWriter.Write('C');
  MyFileWriter.Write(49);  // Chr(49) = '1'
  MyFileWriter.Write(50);  // Chr(50) = '2'
  MyFileWriter.Write(51);  // Chr(51) = '3'
  MyFileWriter.Write(52);  // Chr(52) = '4'

  // Close the writer and the stream
  MyFileWriter.Close;
  MyFileStream.Close;

  // Reopen the stream for reading
  MyFileStream := System.IO.File.Open('C:DelphiBasics.txt',
                                      System.IO.FileMode.Open);

  // Create a BinaryReader to allow the file to be read back
  MyFileReader := System.IO.BinaryReader.Create(MyFileStream);

  // Declare our array to hold the returned data
  SetLength(MyCharArray, 10);

  // Get the file contents 3 characters at a time into our array
  Index := 0;
  while MyFileReader.PeekChar <> -1 do
  begin
    MyFileReader.Read(MyCharArray, index, 3);
    Index := Index + 3;
  end;

  // Display our array contents
  Console.WriteLine(MyCharArray);

  // Close the reader and the stream
  MyFileReader.Close;
  MyFileStream.Close;

  Console.Readline;
end.
Show full unit code
  ABC1234
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author