feof

Tests whether the read position is at the end of a file.

Category: Data Import and Analysis > Data Import and Export

Syntax

tf = feof(fid)

Returns true once a read has hit the end of the file.

Description

The usual use is a read loop: while ~feof(fid). The indicator is set by a read that reaches the end, not merely by sitting at the last byte, so the loop reads every line, including a last line with no trailing newline.

Input arguments

fid
File identifier from fopen.

Output arguments

tf
Logical scalar.

Example

Read a file line by line

fid = fopen('w.txt', 'w');
fprintf(fid, 'alpha\nbeta\ngamma');
fclose(fid);
fid = fopen('w.txt');
while ~feof(fid)
    fprintf('[%s]\n', fgetl(fid));
end
fclose(fid);
Output
[alpha]
[beta]
[gamma]

See also

fgetl fgets fopen frewind