Home  |  Delphi .net Home  |  System.IO.BinaryReader  |  PeekChar Method
PeekChar  
Method  
Returns (peeks at) the next stream byte without updating stream position
BinaryReader Class
System.IO NameSpace
CF1.  Function PeekChar ( ) : Integer;
CF : Methods with this mark are Compact Framework Compatible
Description
The PeekChar method returns the next stream character as an integer. The stream position is unaffected. The value returned is -1 if the stream is exhausted.
Notes
Be wary of stream byte values of 255.
Microsoft MSDN Links
System.IO
System.IO.BinaryReader
 
 
Stopping reading when a certain value is encountered
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

var
  MyFileStream  : System.IO.FileStream;
  MyFileWriter  : System.IO.BinaryWriter;
  MyFileReader  : System.IO.BinaryReader;
  CharAsInteger : 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('ABC');
  MyFileWriter.Write(123);
  MyFileWriter.Write('DEF');

  // 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 until a value of 123 is read
  // Only the 'ABC' string length and decimal values of A,B,C
  // are read.
  while MyFileReader.PeekChar <> 123 do
    Console.WriteLine(MyFileReader.ReadByte.ToString);

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

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