ftell

Returns the current byte position in a file.

Category: Data Import and Analysis > Data Import and Export

Syntax

position = ftell(fid)

Returns the offset from the start of the file.

Description

A newly opened file is at position 0. Reads, writes and fseek move it. You can save a position with ftell and return to it later with fseek(fid, pos, 'bof').

Input arguments

fid
File identifier from fopen.

Output arguments

position
Zero-based byte offset, or -1 on error.

Example

Mark and return

fid = fopen('t.txt', 'w'); fprintf(fid, 'abcdef'); fclose(fid);
fid = fopen('t.txt');
fread(fid, 2);
mark = ftell(fid)
fread(fid, 3);
ftell(fid)
fseek(fid, mark, 'bof');
char(fread(fid, 1))
fclose(fid);
Output
mark = 2
ans = 5
ans = 'c'

See also

fseek frewind fread fopen