This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Move file position indicator to beginning of open file





frewind(fileID) sets the file position indicator to the beginning of a file.


collapse all

Open the following file, perform a read operation, and then move the file position indicator back to the beginning of the file.

Use fopen to open the file, and then use fgetl to read the first two lines.

fid = fopen('badpoem.txt');
tline1 = fgetl(fid)  % read first line 
tline1 = 
'Oranges and lemons,'
tline2 = fgetl(fid)  % read second line
tline2 = 
'Pineapples and tea.'

The previous two read operations moved the position indicator to the beginning of line 3 in the poem. As a result, the next read operation using fgetl returns line 3.

tline3 = fgetl(fid)
tline3 = 
'Orangutans and monkeys,'

To reread the first line of the file. Reset the position indicator using the frewind function, and then perform the read operation.

ans = 
'Oranges and lemons,'

Close the file.


Input Arguments

collapse all

File identifier of an open file, specified as an integer. Before using frewind you must use fopen to open the file and obtain its fileID.

Data Types: double


If the file is on a tape device and the rewind operation fails, frewind does not return an error message.


frewind(fileID) is equivalent to:

fseek(fileID, 0, 'bof');

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Introduced before R2006a