bug0098.pp 2.1 KB

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