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().
- offset
- Number of positions to move in the file.
- origin
- Position from which to begin counting the offset. SEEK_SET is the default.
Outputs
- R
- Scalar 0 or -1
Example
% 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