RunMat
GitHub

frewind — Rewind the file position indicator to the beginning of an open file.

frewind(fid) moves the file position indicator back to the beginning of the file associated with fid. It is equivalent to calling fseek(fid, 0, 'bof') and is commonly used to re-read a file after an initial pass without closing and reopening it.

How frewind works in RunMat

  • frewind(fid) rewinds the file position indicator to byte offset 0 for the file identified by fid.
  • fid must be a non-negative integer returned by fopen. Passing an invalid identifier raises an error.
  • Standard input/output identifiers (0, 1, 2) are silently accepted and treated as a no-op; RunMat does not support seeking on those streams.
  • frewind produces no output value. Assigning the result of frewind to a variable yields an empty output list.
  • After frewind, subsequent calls to fread, fgets, or fscanf start from the beginning of the file.

How frewind runs on the GPU

frewind is a host-only operation. 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.

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 seek. The rewind operation itself executes entirely on the host.

Examples

Write then rewind and read back binary data

fid = fopen('data.bin', 'w+b');
fwrite(fid, [1 2 3], 'double');
frewind(fid);
values = fread(fid, 'double');
fclose(fid);
delete('data.bin')

Read a text file twice without reopening

fid = fopen('notes.txt', 'r');
first_line = fgets(fid);
frewind(fid);
same_line = fgets(fid);
fclose(fid)

Rewind a binary file opened for reading bytes

fid = fopen('payload.bin', 'w+b');
fwrite(fid, uint8(1:6), 'uint8');
frewind(fid);
bytes = fread(fid, 4, 'uint8');
fclose(fid);
delete('payload.bin')

FAQ

What kind of input does frewind accept?

Pass a scalar numeric file identifier returned by fopen. The identifier must be a non-negative integer. Character vectors and strings are not accepted.

Does frewind return a value?

No. frewind is a void function and produces no output. Capturing its result assigns an empty output list.

How does frewind compare to fseek?

frewind(fid) is exactly equivalent to fseek(fid, 0, 'bof'). Use frewind when you only ever need to return to the beginning; use fseek when you need to jump to arbitrary positions.

Can I call frewind on a write-only file?

Yes, as long as the underlying file was opened with a mode that supports seeking (e.g. 'w+' or 'w+b'). Rewinding a write-only stream positions the cursor at the start so subsequent writes overwrite from the beginning.

Can I call frewind on a closed file?

No. Passing a closed or never-opened identifier raises "Invalid file identifier. Use fopen to generate a valid file ID."

These functions work well alongside frewind. Each page has runnable examples you can try in the browser.

fopen, fclose, fread, fwrite, feof, fgets, fileread, filewrite, fprintf

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how frewind works, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.

  • Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
  • Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
  • A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.

Getting started · Benchmarks · Pricing

Try RunMat — free, no sign-up

Start running MATLAB code immediately in your browser.