Move to specified position in file
fseek(
fileID
, offset
, origin
)status
= fseek(fileID
, offset
, origin
)
fseek(
sets
the file position indicator fileID
, offset
, origin
)offset
bytes
from origin
in the specified file.
returns status
= fseek(fileID
, offset
, origin
)0
when
the operation is successful. Otherwise, it returns -1
.
|
Integer file identifier obtained from |
|
Number of bytes to move from |
|
Starting location in the file, specified as a character vector, string scalar or a scalar number:
|
Copy 5 bytes from the file test1.dat
, starting
at the tenth byte, and append to the end of test2.dat
:
% Create files test1.dat and test2.dat % Each character uses 8 bits (1 byte) fid1 = fopen('test1.dat', 'w+'); fwrite(fid1, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'); fid2 = fopen('test2.dat', 'w+'); fwrite(fid2, 'Second File'); % Seek to the 10th byte ('J'), read 5 fseek(fid1, 9, 'bof'); A = fread(fid1, 5, 'uint8=>char'); fclose(fid1); % Append to test2.dat fseek(fid2, 0, 'eof'); fwrite(fid2, A); fclose(fid2);
To move to the beginning of a file, call
frewind(fileID)
This call is identical to
fseek(fileID, 0, 'bof')