W3cubDocs

/Octave

14.2.19 File Positioning

Three functions are available for setting and determining the position of the file pointer for a given file.

pos = ftell (fid)

Return the position of the file pointer as the number of characters from the beginning of the file specified by file descriptor fid.

See also: fseek, frewind, feof, fopen.

fseek (fid, offset)
fseek (fid, offset, origin)
status = fseek (…)

Set the file pointer to the location offset within the file fid.

The pointer is positioned offset characters from the origin, which may be one of the predefined variables SEEK_SET (beginning), SEEK_CUR (current position), or SEEK_END (end of file) or strings "bof", "cof", or "eof". If origin is omitted, SEEK_SET is assumed. offset may be positive, negative, or zero but not all combinations of origin and offset can be realized.

fseek returns 0 on success and -1 on error.

See also: fskipl, frewind, ftell, fopen, SEEK_SET, SEEK_CUR, SEEK_END.

SEEK_SET ()

Return the numerical value to pass to fseek to position the file pointer relative to the beginning of the file.

See also: SEEK_CUR, SEEK_END, fseek.

SEEK_CUR ()

Return the numerical value to pass to fseek to position the file pointer relative to the current position.

See also: SEEK_SET, SEEK_END, fseek.

SEEK_END ()

Return the numerical value to pass to fseek to position the file pointer relative to the end of the file.

See also: SEEK_SET, SEEK_CUR, fseek.

frewind (fid)
status = frewind (fid)

Move the file pointer to the beginning of the file specified by file descriptor fid.

frewind returns 0 for success, and -1 if an error is encountered. It is equivalent to fseek (fid, 0, SEEK_SET).

See also: fseek, ftell, fopen.

The following example stores the current file position in the variable marker, moves the pointer to the beginning of the file, reads four characters, and then returns to the original position.

marker = ftell (myfile);
frewind (myfile);
fourch = fgets (myfile, 4);
fseek (myfile, marker, SEEK_SET);

© 1996–2018 John W. Eaton
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://octave.org/doc/interpreter/File-Positioning.html