What does the feof function do in MATLAB / RunMat?
feof(fid) reports whether the file position indicator associated with fid
has reached the end of the file. It is most commonly used to terminate loops
that repeatedly call fread, fgets, or fscanf.
How does the feof function behave in MATLAB / RunMat?
tf = feof(fid)returns logical1when the file position indicator is at end-of-file and logical0otherwise.fidmust be a non-negative integer returned byfopen. Passing an invalid identifier raises the same error message as other file I/O builtins.- Standard input/output identifiers (
0,1,2) always reportfalsebecause RunMat does not yet expose end-of-file state for those streams. feofpreserves the file position indicator; checking the flag does not modify where subsequent reads continue.- When a file is empty,
feofimmediately returnstruebecause the initial file position coincides with the end of the file.
feof Function GPU Execution Behaviour
feof is a host-only query. If fid resides on the GPU (for example inside a
gpuArray), RunMat gathers the scalar to host memory before consulting the file
registry. File handles always remain on the CPU, so there are no provider hooks
or fusion paths for this builtin. The result is produced on the host as a
logical scalar that mirrors MATLAB behaviour.
GPU residency in RunMat (Do I need gpuArray?)
No. File identifiers are always owned by the host-side registry maintained by
fopen. Wrapping a scalar identifier in gpuArray does not keep the file handle
on the GPU—the runtime gathers the scalar before performing the lookup. This
means you can freely mix host and GPU code when driving file I/O loops, and
feof remains inexpensive regardless of execution tier.
Examples of using the feof function in MATLAB / RunMat
Check for end-of-file while reading line by line
[fid, msg] = fopen('log.txt', 'r');
assert(fid ~= -1, msg);
while ~feof(fid)
line = fgets(fid);
disp(line);
end
fclose(fid);
Detect the end of a binary stream read with fread
[fid, msg] = fopen('payload.bin', 'rb');
assert(fid ~= -1, msg);
while ~feof(fid)
chunk = fread(fid, 1024, 'uint8');
process(chunk);
end
fclose(fid);
Handle empty files gracefully
[fid, msg] = fopen('empty.txt', 'r');
assert(fid ~= -1, msg);
isAtEnd = feof(fid); % returns true immediately because the file is empty
fclose(fid);
Protect against invalid identifiers
try
feof(9999);
catch err
disp(err.message);
end
Guard reads that rely on gpuArray identifiers
fid = gpuArray(int32(fopen('samples.dat', 'rb')));
tf = feof(fid); % argument is gathered automatically before the check
FAQ
What kind of input does feof accept?
Pass a scalar numeric file identifier returned by fopen. The identifier must
be non-negative and finite. Character vectors or strings are not accepted.
Does feof move the file position indicator?
No. The builtin queries the current position and restores it before returning, so subsequent reads resume where they left off.
How does feof behave for standard input/output?
RunMat currently reports false for identifiers 0, 1, and 2 because the
standard streams are not integrated with the file registry. Use other platform
APIs to detect end-of-file on those streams if necessary.
Does feof require a previous read attempt to return true?
No. RunMat compares the current position against the end of the file. As soon
as the indicator reaches the end (for example after reading all bytes or when
the file is empty), feof returns true.
Can I call feof on closed files?
No. Passing a closed or never-opened identifier raises
"Invalid file identifier. Use fopen to generate a valid file ID."
How does feof interact with GPU execution?
feof itself never runs on the GPU. When the identifier is stored in a GPU
array, the runtime gathers it before performing the host-only check.
See Also
fopen, fclose, fread, fwrite, fileread
Source & Feedback
- Source:
crates/runmat-runtime/src/builtins/io/filetext/feof.rs - Found a behavioural difference? Open an issue with a minimal reproduction.