Home  |  Delphi .net Home  |  System.IO.BinaryReader  |  ReadByte Method
ReadByte  
Method  
Reads the next byte from the current stream
BinaryReader Class
System.IO NameSpace
CF1.  Function ReadByte ( ) : Byte;
CF : Methods with this mark are Compact Framework Compatible
Description
If the current stream has not been exhausted, the next byte is returned.
 
The stream position is updated accordingly.
Microsoft MSDN Links
System.IO
System.IO.BinaryReader
 
 
Reading a single byte at a time
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

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

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(12);
  MyFileWriter.Write(34);
  MyFileWriter.Write('ABC');  // Chr(65) Chr(66) Chr(67)

  // 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
  try
    // Infinite loop - we rely on the exception for file end
    while 1 = 1 do
    begin
      SingleByte := MyFileReader.ReadByte;
      Console.WriteLine(SingleByte.ToString);
    end;
  except
    On E : EndOfStreamException do
      Console.WriteLine('File display completed');
  end;

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

  Console.Readline;
end.
Show full unit code
  12
  34
  3
  65
  66
  67
  File display completed
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author