standard.C.io.fseek

fseek is available since version 1.0.

Prototype:

number fseek(number fd, number offset, number origin)

Description:

The fseek function moves the file pointer (if any) associated with fd to a new location that is offset bytes from origin. The next operation on the file takes place at the new location.

The argument origin must be one of the following constants:
SEEK_CUR
Current position of file pointer.
SEEK_END
End of file.
SEEK_SET
Beginning of file.

For files opened in text mode, fseek has limited use, because carriage return-linefeed translations can cause fseek to produce unexpected results. The only fseek operations guaranteed to work on streams opened in text mode are:

Seeking with an offset of 0 relative to any of the origin values.
Seeking from the beginning of the file with an offset value returned from a call to ftell.

Also in text mode, CTRL+Z is interpreted as an end-of-file character on input. In files opened for reading/writing, fopen and all related routines check for a CTRL+Z at the end of the file and remove it if possible. This is done because using fseek and ftell to move within a file that ends with a CTRL+Z may cause fseek to behave improperly near the end of the file.

Return value:

If successful, fseek returns 0. Otherwise, it returns a nonzero value. On devices incapable of seeking, the return value is undefined.

Example
import standard.C.io
import standard.C.string

define FILENAME     "test.txt"

class Main {
     function Main() {
          var fd; 
          var result;
          var buffer = "";          

          // Open file for read and write
          if (fd = fopen(FILENAME, "w+")) {               
               buffer = "The fseek begins here: This is the file 'fseek.out'.\n";
               fwrite(buffer, 1, strlen(buffer), fd);
               result = fseek(fd, 23, SEEK_SET);
               if (result)
                    echo "Fseek failed\n";
               else {
                    buffer = "";
                    echo "File pointer is set to the middle of the file\n";
                    fgets(buffer, 30, fd);
                    echo buffer;
               }
               fclose(fd);               
          } else
               echo "Problem opening the file\n";
          
     }
}  

Results 
File pointer is set to the middle of the file
This is the file 'fseek.out'.