Subject: Last byte of a file 90. ***** Q: How can I quickly read the last byte of a file? A: Below is the code for a relevant procedure. It has a number of instructive details for you to look into. It is easy to expand this procedure into showing any byte counted from the end by substituting the 1 in Seek (f, fs-1) to the inverted position, and by taking care that the position is not outside the file. procedure LASTBYTE (fname : string; var lb : byte); var f : file; { Use an untyped file designation } fmSave : byte; { To push and pop the FileMode } fs : longint; { For file size } begin fmSave := FileMode; { Push the original FileMode } FileMode := 0; { To enable reading also read-only files } Assign (f, fname); {$I-} Reset (f, 1); {$I+} { Open file and set record size to 1 } if IOResult <> 0 then begin writeln ('Error opening file ', fname); halt; end; fs := FileSize(f); { Get the size of the file } if fs = 0 then begin writeln ('Empty file ', fname); halt; end; Seek (f, fs-1); { Position to the last byte of the file } BlockRead (f, lb, 1); { Read the value of the position into lb } Close (f); { Close the file } FileMode := fmSave; { Pop the original FileMode } end; (* lastbyte *) --------------------------------------------------------------------