tb0084.pp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. { Create temp }
  13. assign(F,'tb0084.tmp'); { Assign F to itself }
  14. rewrite(f);
  15. for l:=1 to 100 do
  16. writeln('Hello world');
  17. close(f);
  18. assign(F,'tb0084.tmp'); { Assign F to itself }
  19. reset(F); { Open it (as a textfile) }
  20. ReadLn(F); { Just read some lines }
  21. ReadLn(F);
  22. ReadLn(F);
  23. FileRec((@F)^).Mode:=fmInOut; { Set to binary mode }
  24. { (The (@F)^ part is to let TP 'forget' the type of the structure, so }
  25. { you can type-caste it to everything (note that with and without (@X)^ }
  26. { can give a different value, longint(bytevar) gives the same value as }
  27. { bytevar, while longint((@bytevar)^) gives the same as }
  28. { longint absolute Bytevar (i.e. all 4 bytes in a longint are readed }
  29. { from memory instead of 3 filled with zeros))) }
  30. FileRec((@F)^).RecSize:=1; { Set record size to 1 (a byte)}
  31. L:=(FilePos(File((@F)^))-TextRec(F).BufEnd)+TextRec(F).BufPos;
  32. {... This line didn't work the last time I tried, it chokes on the "File"
  33. typecasting thing.}
  34. { Get the fileposition, subtract the already readed buffer, and add the }
  35. { position in that buffer }
  36. TextRec(F).Mode:=fmInput; { Set back to text mode }
  37. TextRec(F).BufSize:=SizeOf(TextBuf); { BufSize overwritten by RecSize }
  38. { Doesn't work with SetTextBuf! }
  39. ReadLn(F,S); { Read the next line }
  40. WriteLn('Next line:',S); { Display it }
  41. FileRec((@F)^).Mode:=fmInOut; { Set to binary mode }
  42. FileRec((@F)^).RecSize:=1; { Set record size to 1 (a byte)}
  43. Seek(File((@F)^),L); { Do the seek }
  44. {... And again here.}
  45. TextRec(F).Mode:=fmInput; { Set back to text mode }
  46. TextRec(F).BufSize:=SizeOf(TextBuf); { Doesn't work with SetTextBuf! }
  47. TextRec(F).BufPos:=0; TextRec(F).BufEnd:=0; { Reset buffer counters }
  48. ReadLn(F,S); { Show that it worked, the same }
  49. WriteLn('That line again:',S); { line readed again! }
  50. Close(F); { Close it }
  51. Erase(F);
  52. end.