feof — Query end-of-file state for file identifiers in MATLAB and RunMat.
feof(fid) reports the end-of-file indicator associated with the open file identified by fid. It returns a logical scalar. Opening a file initializes the indicator to false; a read that encounters end-of-file sets it, and successful repositioning clears it.
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. |
| — | Internal control-flow conversion failed. | feof: internal error |
How feof works
tf = feof(fid)returns logical1only after a prior read on that open file encountered end-of-file; merely reaching the exact final byte does not set the indicator.- The MATLAB-compatible identifier form is a finite integer-valued double scalar returned by
fopen, checked against the runtime i32 identifier range before registry access. - Typed integer identifiers across all eight built-in integer classes, logical identifiers, and provider-resident identifiers are three independently gated RunMat extensions. Compatibility preflight occurs before provider gathering or registry access.
- Standard input/output identifiers (
0,1,2) always reportfalsebecause RunMat does not yet expose end-of-file state for those streams. feofonly queries registry state: it does not seek, inspect file length, contact a provider after host conversion, or modify the file position.- A newly opened empty file reports
false. The first read attempt that encounters end-of-file sets the indicator to true. frewindand other successful positioning operations clear the end-of-file indicator. Closing the file removes its state with the registry entry.- The compatibility classification is grounded in the linked public documentation and remains evidence-open for additional release-specific findings.
Does RunMat run feof on the GPU?
feof is a host-only state query with no provider hooks or fusion path. In RunMat extension mode, a resident identifier is compatibility-gated before gathering; the resulting host scalar is range-checked before the registry is queried. The result is always a host logical scalar.
GPU memory and residency
No. File handles and end-of-file state are owned by the host registry. A provider-resident scalar identifier is a gated RunMat extension; compatibility preflight runs before the provider is contacted, then the scalar is gathered for exact identifier validation and the host registry query.
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)Observe read-driven state for an empty file
[fid, msg] = fopen('empty.txt', 'r');
assert(fid ~= -1, msg);
assert(~feof(fid));
fgets(fid); % encounters end-of-file
assert(feof(fid));
frewind(fid);
assert(~feof(fid));
fclose(fid)Protect against invalid identifiers
try
feof(9999);
catch err
disp(err.message);
endChecking end-of-file with typed and resident identifiers
fid = gpuArray(int32(fopen('samples.dat', 'rb')));
tf = feof(fid); % resident and typed-integer extension gates apply before gatheringUsing 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?⌄
The MATLAB-compatible form is a finite integer-valued double scalar returned by fopen. RunMat mode can additionally enable typed integer, logical, and provider-resident identifier extensions. Character vectors and strings are not accepted.
Does feof move the file position indicator?⌄
No. The builtin reads the registry's end-of-file flag and performs no seek or length query, so subsequent reads resume exactly 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?⌄
Yes. Reaching the exact end position is not sufficient. A read operation must attempt to read at end-of-file; successful repositioning such as frewind clears the flag.
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
Filetext
fclose · fgetl · fgets · fileread · filewrite · fopen · fprintf · fread · frewind · fwrite · readlines · writelines
Repl Fs
addpath · cd · copyfile · delete · dir · exist · fileattrib · fileparts · fullfile · genpath · getenv · getpref · isenv · isfile · isfolder · ispref · ls · matlabroot · memmapfile · mkdir · movefile · open · opentoline · path · pathsep · pcode · pwd · readstruct · rehash · restoredefaultpath · rmdir · rmpath · run · savepath · setenv · setpref · system · tempdir · tempname · uigetdir · uigetfile · uiputfile · unsetenv · userpath · what · winqueryreg · xmlread · xmlwrite
Tabular
arrayDatastore · csvread · csvwrite · detectImportOptions · dlmread · dlmwrite · fileDatastore · parquetDatastore · parquetinfo · parquetread · readcell · readmatrix · readtable · readtimetable · spreadsheetImportOptions · writecell · writematrix · writetable · writetimetable · xlsread · xlswrite
Import
Json
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.