C fgetpos() function

C library function - fgetpos()

The fgetpos() function is used to store the current position of the file pointer that is associated with stream into the object pointed to by pos. The value pointed to by pos can be used later in a call to fsetpos() to reposition the stream.


int fgetpos(FILE *stream, fpos_t *pos)

fgetpos() Parameters:

Name Description Required /Optional
stream Identifies an address for a file descriptor, which is an area of memory associated with an input or output stream. Required
pos This is the pointer to a fpos_t object. Required

Return value from fgetpos()

  • Upon successful completion, fgetpos() shall return 0; otherwise, it shall return a non-zero value and set errno to indicate the error.

Example: fgetpos() function

Following example opens the file test.txt for reading and stores the current file pointer position into the variable position.

#include <stdio.h>
#include <string.h>

int main () {
   FILE *fp;
   fpos_t position;

   fp = fopen("file.txt","w+");
   fgetpos(fp, &position);
   fputs("Hello, World!", fp);
   fsetpos(fp, &position);
   fputs("This is going to override previous content", fp);


Current position of file pointer found!

Errors: The fgetpos() function shall fail if:-

  • The file descriptor underlying stream is not valid
  • The current value of the file position cannot be represented correctly in an object of type fpos_t.
  • The file descriptor underlying stream is associated with a pipe, FIFO, or socket.

C Programming Code Editor:

Previous C Programming: C fflush()
Next C Programming: C fopen()

Follow us on Facebook and Twitter for latest update.