fseek
Moves the file position to a given byte offset.
Category: Data Import and Analysis > Data Import and Export
Syntax
status = fseek(fid, offset, origin)
Moves offset bytes relative to origin.
Description
The next read or write starts at the new position. fseek(fid, 0, 'eof') followed by ftell gives the file size in bytes. Use a negative offset with 'eof' to look at the end of a file, such as the last few characters.
Input arguments
offset- Number of bytes to move; may be negative.
origin'bof'start of file,'cof'current position,'eof'end of file.
Output arguments
status0on success,-1on failure.
Example
File size and the last bytes
fid = fopen('w.txt', 'w'); fprintf(fid, 'alpha beta gamma'); fclose(fid);
fid = fopen('w.txt');
fseek(fid, 0, 'eof');
sz = ftell(fid)
fseek(fid, -5, 'eof');
tail = fread(fid, 5, '*char')'
fseek(fid, 6, 'bof');
word = fread(fid, 4, '*char')'
fclose(fid);
Output
sz = 16
tail = 'gamma'
word = 'beta'