feof — Query end-of-file state for file identifiers in MATLAB and RunMat.
feof(fid) reports whether the file position indicator for fid is at end-of-file. It returns a logical scalar and is typically used to terminate loops that read with fread, fgets, or related I/O functions under MATLAB semantics.
Syntax
tf = feof(fid)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
fid | NumericScalar | Yes | — | File identifier opened by fopen. |
Returns
| Name | Type | Description |
|---|---|---|
tf | LogicalArray | True when positioned at end-of-file. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:feof:InvalidInput | Input identifier is malformed or out of range. | feof: invalid file identifier input |
RunMat:feof:InvalidIdentifier | Identifier does not refer to an open file handle. | feof: invalid file identifier. Use fopen to generate a valid file ID. |
RunMat:feof:IoFailure | File-position or length query fails. | feof: file I/O query failed |
| — | Internal control-flow conversion failed. | feof: internal error |
How feof works
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.
Does RunMat run feof on the GPU?
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 memory and residency
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
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);
endGuard reads that rely on gpuArray identifiers
fid = gpuArray(int32(fopen('samples.dat', 'rb')));
tf = feof(fid); % argument is gathered automatically before the checkUsing feof with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how feof changes the result.
Run a small feof example, explain the result, then change one input and compare the output.
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.
Related Io functions
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how feof is executed, line by line, in Rust.
- View the source for feof in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.