fseek

Sets the file pointer to a position in fileID. Returns 0 if successful; returns -1 if unsuccessful.

Syntax

R = fseek(fileID, offset)

R = fseek(fileID, offset, origin)

Inputs

fileID
File ID returned from fopen().
Type: double | integer
offset
Number of positions to move in the file.
Type: double | integer
origin
Position from which to begin counting the offset. SEEK_SET is the default.
SEEK_CUR or 'cof' for current position in file.
SEEK_SET or 'bof' for beginning of file.
SEEK_END or 'eof' for end of file.

Outputs

R
Scalar 0 or -1

Example

fseek and related functions:
% example for fseek, ftell, SEEK_END, 'eof', SEEK_SET, 'bof', SEEK_CUR, 'cof'
fileID = fopen('testfile7', 'r')
printf('starting position in file %d\n', ftell(fileID))

% go to end of file and show position
printf('fseek result %d\n', fseek(fileID, 0, SEEK_END))	% can use SEEK_END or 'eof'
printf('after SEEK_END position in file %d\n', ftell(fileID))

% return to begining of file and show position
printf('fseek result %d\n', fseek(fileID, 0, SEEK_SET))	% can use SEEK_SET or 'bof'
printf('after SEEK_SET position in file %d\n', ftell(fileID))

% change position +8 characters forward and show position
printf('fseek result %d\n', fseek(fileID, +8, SEEK_CUR))	% can use SEEK_CUR or 'cof'
printf('after SEEK_CUR +8 position in file %d\n', ftell(fileID))

% change position 4 characters backward and show position
printf('fseek result %d\n', fseek(fileID, -4, SEEK_CUR))	% can use SEEK_CUR or 'cof'
printf('after SEEK_CUR -4 position in file %d\n', ftell(fileID))

printf('fclose result %d\n', fclose(fileID))
fileID = 3
starting position in file 0
fseek result 0
after SEEK_END position in file 15
fseek result 0
after SEEK_SET position in file 0
fseek result 0
after SEEK_CUR +8 position in file 8
fseek result 0
after SEEK_CUR -4 position in file 4
fclose result 0