tb0084.pp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. { Old file: tbs0098.pp }
  2. { File type casts are not allowed (works in TP7) OK 0.99.1 (FK) }
  3. program Test;
  4. { Show how to seek to an OFFSET (not a line number) in a textfile, }
  5. { without using asm. Arne de Bruijn, 1994, PD }
  6. uses Dos; { For TextRec and FileRec }
  7. var
  8. F:text;
  9. L:longint;
  10. S:string;
  11. begin
  12. assign(F,'tbs/tb0084.pp'); { Assign F to itself }
  13. reset(F); { Open it (as a textfile) }
  14. ReadLn(F); { Just read some lines }
  15. ReadLn(F);
  16. ReadLn(F);
  17. FileRec((@F)^).Mode:=fmInOut; { Set to binary mode }
  18. { (The (@F)^ part is to let TP 'forget' the type of the structure, so }
  19. { you can type-caste it to everything (note that with and without (@X)^ }
  20. { can give a different value, longint(bytevar) gives the same value as }
  21. { bytevar, while longint((@bytevar)^) gives the same as }
  22. { longint absolute Bytevar (i.e. all 4 bytes in a longint are readed }
  23. { from memory instead of 3 filled with zeros))) }
  24. FileRec((@F)^).RecSize:=1; { Set record size to 1 (a byte)}
  25. L:=(FilePos(File((@F)^))-TextRec(F).BufEnd)+TextRec(F).BufPos;
  26. {... This line didn't work the last time I tried, it chokes on the "File"
  27. typecasting thing.}
  28. { Get the fileposition, subtract the already readed buffer, and add the }
  29. { position in that buffer }
  30. TextRec(F).Mode:=fmInput; { Set back to text mode }
  31. TextRec(F).BufSize:=SizeOf(TextBuf); { BufSize overwritten by RecSize }
  32. { Doesn't work with SetTextBuf! }
  33. ReadLn(F,S); { Read the next line }
  34. WriteLn('Next line:',S); { Display it }
  35. FileRec((@F)^).Mode:=fmInOut; { Set to binary mode }
  36. FileRec((@F)^).RecSize:=1; { Set record size to 1 (a byte)}
  37. Seek(File((@F)^),L); { Do the seek }
  38. {... And again here.}
  39. TextRec(F).Mode:=fmInput; { Set back to text mode }
  40. TextRec(F).BufSize:=SizeOf(TextBuf); { Doesn't work with SetTextBuf! }
  41. TextRec(F).BufPos:=0; TextRec(F).BufEnd:=0; { Reset buffer counters }
  42. ReadLn(F,S); { Show that it worked, the same }
  43. WriteLn('That line again:',S); { line readed again! }
  44. Close(F); { Close it }
  45. end.