# seek

Definition: | integer i0 = seek(integer fn, integer pos)
Description: | Seek (move) to any byte position in the file fn or to the end of file if pos is -1. For each open file there is a current byte position that is updated as a result of I/O operations on the file. The initial file position is 0 for files opened for read, write or update. The initial position is the end of file for files opened for append. Returns SEEK_OK (0) if the seek was successful, and non-zero if unsuccessful. It is possible to seek past the end of a file. If you seek past the end of the file, and write some data, undefined bytes will be inserted into the gap between the original end of file and your new data.
pwa/p2js: | Not supported.
Comments: | After seeking and reading (writing) a series of bytes, you may need to call seek() explicitly before you switch to writing (reading) bytes, even though the file position should already be what you want. This function is normally used with files opened in binary mode. In text mode, DOS converts CR LF to LF on input, and LF to CR LF on output, which can cause great confusion when you are trying to count bytes.
Example: |
    integer fn = open("mydata", "rb")
    -- read and display first line of file 3 times:
    for i=1 to 3 do
        puts(1, gets(fn))
        if seek(fn, 0)!=SEEK_OK then
            puts(1, "rewind failed!\n")
        end if
    end for
Implementation: | via :%opSeek / fseek() in builtins\VM\pfileioN.e (an autoinclude) - be warned however it is low-level complicated stuff that you do not need to know.
See Also: | where, open