Relative positioning of the data pointer
Seek[%] #n, lpos (command)
RelSeek[%] #n, lpos
SeekEnd #n
lpos = Seek[%](#n) (function)
n:integer expression; channel number
lpos:Large expression (or integer for xxx% commands)
Seek, RelSeek and SeekEnd enable access to index sequential files with channel numbers from 0 to 511 previously opened with Open; they can not be used with peripheral devices.
The Seek command moves the file data pointer to the position specfied in the lpos value; RelSeek performs a similar task but moves the pointer lpos places further on (or back if lpos is negative - RelSeek only) from the pointer's current position. Care should be taken with both these commands not to move the pointer beyond either the start or the end of file as this will result in an error.
SeekEnd has but one task and that is to move the file data pointer to the end of the file.
Finally, the position of the file data pointer can be returned by using the Seek function.
With all the above commands and functions, when the suffix % is used, it restricts their use to files no greater than 2GB and returns values as 32-bit integers; these variants are included for compatibility reasons.
// Create Test File
Local a$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", b&
BSave App.Path & "\Test.Dat", V:a$, 26
// Open Test File
Open App.Path & "\test.dat" for Binary As # 1
// Reading a byte moves the pointer one place on...
a$ = Chr(Inp|(# 1)) : Print Seek(# 1)
// ...while reading a word moves the pointer two places on...
b& = Inp&(# 1) : Print Seek(# 1)
// .. and so on.
// To move to the tenth byte...
Seek # 1, 10 : Print Seek(# 1)
// ...and to move it six bytes further on...
RelSeek # 1, 6 : Print Seek(# 1)
// ...and then two bytes back...
RelSeek # 1, -3 : Print Seek(# 1)
// ...which brings us to position 13 which prints N
Print Chr(Inp|(# 1))
// Then, off to the end of the file...
SeekEnd # 1 : Print Seek(# 1)
// ...then back to the beginning using either...
RelSeek # 1, -26 : Print Seek(# 1)
// ...or...
Seek # 1, 0
// Finally, some changes to the file...
// ... replacing F with an asterisk...
Seek # 1, 6 : Out| # 1, Asc("*")
// ... and P (pos 15) with an underscore...
RelSeek # 1, 8 : Out| # 1, Asc("_")
// ...and then read and print the file contents...
Seek # 1, 0 : Input # 1;a$ : Print a$
//...and then show the pointer
Print Seek(# 1)
Close # 1
Kill App.Path & "\Test.Dat"
The Seek function is synonymous with Loc.
Sometimes, Relseek does not work well in large files and can cause the file pointer to move to the wrong place. In these circumstances, use Seek #n, Loc(#n) + bytes where n is the file number and bytes is the number of bytes you wish to move. This workaround works for backward moves as well: just replace the plus sign with a minus.
{Created by Sjouke Hamstra; Last updated: 08/03/2018 by James Gaite}