Home  |  Delphi .net Home  |  System.IO.FileStream  |  SetLength Method
SetLength  
Method  
Sets the length of the current stream
FileStream Class
System.IO NameSpace
CF1.  Procedure SetLength ( StreamSize : Int64; ) ;
CF : Methods with this mark are Compact Framework Compatible
Description
The current FileStream is contracted or expanded to the specified StreamSize.
 
Data is lost from the end of the stream is it is contracted.
 
If expanded, the added bytes have no predictable value.
Microsoft MSDN Links
System.IO
System.IO.FileStream
 
 
Increasing the size of the stream
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

var
  Stream        : System.IO.FileStream;
  ByteAsInteger : Integer;

begin
  // Open the file for writing
  Stream := System.IO.FileStream.Create('C:\DelphiBasics.txt',
                                        FileMode.Create);

  // Write to the file
  Stream.WriteByte(65);  // Chr(65) = 'A'
  Stream.WriteByte(66);  // Chr(66) = 'B'
  Stream.WriteByte(67);  // Chr(67) = 'C'
  Stream.WriteByte(68);  // Chr(68) = 'D'
  Stream.WriteByte(69);  // Chr(69) = 'E'

  // Now set the stream to a length of 10 bytes
  Stream.SetLength(10);

  // Move to the last byte of the file
  Stream.Seek(-1, SeekOrigin.End);

  // And write a new value
  Stream.WriteByte(70);  // Chr(70) = 'F'

  // And go to the beginning again
  Stream.Seek(0, SeekOrigin.Begin);

  // Display the contents
  ByteAsInteger := Stream.ReadByte;
  while ByteAsInteger >= 0 do
  begin
    Console.Write(Char(ByteAsInteger));
    ByteAsInteger := Stream.ReadByte;
  end;

  // Close the stream
  Stream.Close;

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