home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / textfile.swg / 0022_Text file position.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-05-25  |  3.5 KB  |  116 lines

  1. {
  2.  JK> I've started out in Pascal and need some information on how
  3.  JK> to read from a certain point in a file, say line 3.  How
  4.  JK> would I set the pointer to line 3 to read into a variable?
  5.  
  6.  BvG> A seek does not work on textfiles.
  7.  
  8.  Here, this will assist you. originally in a Pascal Newsletter, so it must
  9.  be PD.
  10.  
  11. ---------------------------------------- CUT HERE --------------------------
  12. }
  13. Unit TextUtl2;  (* Version 1.0 *)
  14.  
  15. {Lets you use typed-file operators on TEXT files.  Note that I've cut out MOST}
  16. {of the documentation so as to make it more practical for the PNL.  I strongly}
  17. {advise that you get in touch with the author at the address below (I haven't)}
  18. {It's called TEXTUTL2 because it's a rewrite of an earlier unit called        }
  19. {TEXTUTIL which had some nasty limitations.                                   }
  20.  
  21. {Both files can be FREQed from 3:634/384.0 as TEXTUT*.*, and I strongly       }
  22. {recommend that you do so.                                                    }
  23.  
  24. {I tried looking up the author's telephone number, but Telecom says the number}
  25. {is silent.  Oh well.                                                         }
  26.  
  27. {If you're having trouble, netmail me (Mitch Davis) at 3:634/384.6            }
  28.  
  29.  
  30. (*
  31. Author: Rowan McKenzie  28/12/88
  32.         35 Moore Ave, Croydon, Vic, Australia
  33.  
  34. These 3 routines are improvements to Tim Baldock's TEXTUTIL.PAS unit.
  35. I can be contacted on: Eastwood, Amnet or Tardis BBS (Melbourne Australia)
  36. *)
  37.  
  38. Interface
  39.  
  40. Uses Dos;
  41.  
  42. Procedure TextSeek     (Var F : Text; Offset : Longint);
  43. Function  TextFileSize (Var F : Text): LongInt;
  44. Function  TextFilePos  (Var F : Text): LongInt;
  45.  
  46. Implementation
  47.  
  48. Procedure TextSeek(Var F : Text; Offset : Longint);
  49.  
  50. { seek char at position offset in text file f}
  51.  
  52. var BFile    : File of byte absolute F;  (* Set up File for Seek *)
  53.     BFileRec : FileRec absolute Bfile;
  54.     TFileRec : TextRec Absolute F;
  55.     OldRecSize : Word;
  56.     oldmode : word;
  57.  
  58. Begin
  59.   With BfileRec do Begin
  60.     oldmode:=mode;
  61.     Mode := FmInOut;         (* Change file mode so Turbo thinks it is *)
  62.     OldRecSize := RecSize;   (* dealing with a untyped file.           *)
  63.     RecSize := 1;            (* Set the Record size to 1 byte.         *)
  64.     Seek(Bfile,Offset);      (* Perform Seek on untyped file.          *)
  65.     Mode := oldmode;         (* Change file mode back to text so that  *)
  66.     RecSize := OldRecSize;   (* normal text operation can resume.      *)
  67.   end;
  68.   TfileRec.BufPos := TfileRec.BufEnd; (* Force next Readln.              *)
  69. end; {textseek}
  70.  
  71. Function TextFileSize(Var F : Text): LongInt;
  72.  
  73. { determine size of text file f in bytes}
  74.  
  75. var BFile:File of byte absolute F;
  76.     BFileRec:FileRec absolute Bfile;
  77.     OldRecSize:Word;
  78.     oldmode:word;
  79.  
  80. Begin
  81.   With BfileRec do Begin
  82.     oldmode:=mode;
  83.     Mode := FmInOut;
  84.     OldRecSize := RecSize;
  85.     RecSize := 1;
  86.     TextFileSize := FileSize(Bfile);
  87.     Mode := oldmode;
  88.     RecSize := OldRecSize;
  89.   end;
  90. end; {textfilesize}
  91.  
  92.  
  93. Function Textfilepos(Var F : Text): LongInt;
  94.  
  95. { determine current position (in bytes) in text file f}
  96.  
  97. var BFile:File of byte absolute F;
  98.     BFileRec:FileRec absolute Bfile;
  99.     TFileRec:TextRec Absolute F;
  100.     OldRecSize:Word;
  101.     oldmode:word;
  102.  
  103. Begin
  104.   With BfileRec do Begin
  105.     oldmode:=mode;
  106.     Mode := FmInOut;
  107.     OldRecSize := RecSize;
  108.     RecSize := 1;
  109.     textfilepos := Filepos(Bfile)-tfilerec.bufend+tfilerec.bufpos;
  110.     Mode := oldmode;
  111.     RecSize := OldRecSize;
  112.   end;
  113. end; {textfilepos}
  114.  
  115. end.
  116.