| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
fseek(fileID, offset, origin)
status = fseek(fileID, offset, origin)
fseek(fileID, offset, origin) sets the file position indicator offset bytes from origin in the specified file.
status = fseek(fileID, offset, origin) returns 0 when the operation is successful. Otherwise, it returns -1.
fileID |
Integer file identifier obtained from fopen. |
offset |
Number of bytes to move from origin. Can be positive, negative, or zero. The n bytes of a given file are in positions 0 through n-1. |
origin |
Starting location in the file:
|
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 btye ('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')
fclose | feof | ferror | fopen | frewind | ftell
![]() | fscanf (serial) | ftell | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |