Home  |  Delphi .net Home  |  System.IO.BinaryReader  |  ReadBoolean Method
ReadBoolean  
Method  
Reads the next byte from the current stream as a Boolean
BinaryReader Class
System.IO NameSpace
CF1.  Function ReadBoolean ( ) : Boolean;
CF : Methods with this mark are Compact Framework Compatible
Description
If the current stream has not been exhausted, the next byte is returned as a Boolean. Booleans always take up 1 byte. A value of 0 means false, other values are treated as true.
 
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;

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(false);
  MyFileWriter.Write(true);
  MyFileWriter.Write(0);      // False
  MyFileWriter.Write(1);      // True
  MyFileWriter.Write(123);    // True

  // 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);

  // Read all stream bytes as Booleans
  try
    // Infinite loop : we end via the EndOfStreamException
    while 1 = 1 do
      if MyFileReader.ReadBoolean
      then Console.WriteLine('True')
      else Console.WriteLine('False');
  except
    On E : Exception do
      Console.WriteLine('All done');
  end;

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

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