liveBook · Manning

Ftell

As explained in Modern C, Third Edition

ftell is a C standard-library function that reports a stream's current file position as a byte offset from the beginning of the file.

ftell reports a stream’s current file position as a byte offset from the beginning of the file. Its declaration returns a long int and operates on the position shared by reading and writing. section 14.6

Inspecting a stream position

long int position = ftell(stream); /* position is measured from the file start */ Used with fseek, ftell helps move forward or backward through a binary stream while fread and fwrite advance the position after successful transfers. section 14.6

 size_t fread(void* restrict ptr, size_t size, size_t nmemb,
              FILE* restrict stream);
 size_t fwrite(void const*restrict ptr, size_t size, size_t nmemb,
               FILE* restrict stream);
 int fseek(FILE* stream, long int offset, int whence);
 long int ftell(FILE* stream);

The related declarations place ftell beside fread, fwrite, and fseek, showing the small interface used to inspect and control binary-stream positions. section 14.6

Use ftell with streams opened in binary mode when portability matters; text-mode end-of-line transformations can make binary positioning unreliable across platforms. section 14.6

How Modern C, Third Edition covers it

How deep it goes
Defines the returned offset and its relationship to stream movement, but does not detail failure handling or implementation internals.
Examples
Uses binary-file reading and writing examples that rely on the stream’s advancing file position.
What it assumes
Assumes familiarity with C streams and FILE pointers while establishing binary-mode requirements for portable positioning.
What you can do after
Operate/configure binary streams by querying their current file position and coordinating movement with fseek.
How it connects
Connects ftell with binary streams, fread, fwrite, and fseek.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.