Home  |  Delphi .net Home  |  System.IO.BinaryReader  |  ReadUInt16 Method
  
ReadUInt16  
Method  
Reads the next 2 bytes from the current stream as an UInt16 number
BinaryReader Class
System.IO NameSpace
CF1.  Function ReadUInt16 : UInt16;
CF : Methods with this mark are Compact Framework Compatible
Description
If the current stream has enough bytes remaining, the next 2 bytes are returned as a UInt16 number.
 
The stream position is updated accordingly.
Microsoft MSDN Links
System.IO
System.IO.BinaryReader
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 
 
A simple example
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

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

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
  MyUInt16 := 123;
  MyFileWriter.Write(MyUInt16);
  MyUInt16 := 456;
  MyFileWriter.Write(MyUInt16);

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

  // Try to read MyUInt16 values from the file
  try
    while 1 = 1 do
    begin
      MyUInt16 := MyFileReader.ReadUInt16;
      Console.WriteLine(MyUInt16.ToString);
    end;
  except
    On E : Exception do
      Console.WriteLine('End of file');
  end;

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

  Console.Readline;
end.
   123
   456
   End of file
 
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Contact the author