TIP: Click on subject to list as thread! ANSI
echo: delphi
to: PETER COLLIS
from: BRYAN SMITH
date: 1996-05-31 21:07:00
subject: Help

On 27 May 96 Peter Collis said to Bryan Smith...
 PC> overcoming it by first opening the input text file using FileOpen()
 PC> then doing a seek to the end of the file to get it's size closing it
 PC> and re-opening it as a textfile, to get the file pointer position i
 PC> just used a longint and added the length of each line read +2 to it.
There is a trick I saw first in Clipper that you might be able to make use
of here.  Suppose the file is open as binary, but that you want to do the
equivalent of a ReadLn.  The idea is to read in a buffer from the file,
large enough so you know that it will be longer than the longest line that
will be encountered.  Then scan the buffer until you find the first CR/LF.
It is now easy to calculate how much you must move the file pointer back so
that it is placed immediately after the CR/LF, i.e. positioned ready to read
the next line.  Then you pass back to the calling program just that part of
the buffer that lies ahead of the CRLF.
This may be exactly what happens deep-down when you make a ReadLn call in
Pascal/Delphi.
Regarding the fixed-record-length file you are creating in order to do a
binary search for part numbers.  Is there any chance that you could use a
Delphi TStringList instead, to do it in-core ?  Or build your own linked
list from scratch ?
Back to the binary read, here is some (TP 7.0) code you can play with.  No
guarantees !
FUNCTION FReadLn(VAR fBin : FILE; VAR InputLine : String) : Boolean;
CONST
  nLineLength = 255;
  cdelim : String[2] = #13+#10;
VAR
  nCurPos : LongInt;       { Current position in file }
  nFileSize : LongInt;     { The size of the file }
  L1 : LongInt;
  nChrsToRead : Word;      { Number of chars. to read }
  nChrsRead : Word;        { Number of characters actually read }
  strBuffer : String;
  nEOLPos : Integer;       { Position of EOL in strBuffer }
  more : Boolean;
BEGIN
  cdelim := #13+#10;
  nCurPos := FilePos(fBin);
  nFileSize := FileSize(fBin);
  { Make sure no attempt is made to read past EOF ... }
  nChrsToRead := nLineLength;
  L1 := nFileSize-nCurPos;
  IF L1 < nChrsToRead THEN nChrsToRead := L1;
  InputLine := '';
  more := nChrsToRead > 0;
  IF more THEN
  BEGIN
    BlockRead(fBin, strBuffer[1], nChrsToRead, nChrsRead);
    strBuffer[0] := Chr(nChrsRead);
    { Check for error condition ... }
    IF NOT(nChrsRead = nChrsToRead) THEN nChrsToRead := 0;
    nEOLPos := Pos(cdelim, strBuffer);
    { Update buffer and current file position ... }
    IF nEOLPos = 0 THEN  { no CRLF }
    BEGIN
      InputLine := Copy(strBuffer, 1, nChrsRead);
      nCurPos := nCurPos+nChrsRead;
      { no crlf found, so (ignoring very long line) EOF ... }
      more := False;
    END
    ELSE { found CRLF }
    BEGIN
      InputLine := Copy(strBuffer, 1, nEOLPos -1);
      nCurPos := nCurPos+nEOLPos+Length(cdelim)-1;
      Seek(fBin, nCurPos);
    END;
    FReadLn := NOT more;
  END; { if more }
END;
--- PPoint 2.00
---------------
* Origin: Kingston, Canada (1:249/109.11)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.