C Standard Library

C <stdio.h> - fpos_t Type



The C <stdio.h> fpos_t type is a non-array object type which is used to specify a position within a file. An object of this type is capable of specifying uniquely any position within a file.

The information in a fpos_t object is usually filled by calling fgetpos() function, which takes a pointer to an object of this type as argument. The content of a fpos_t object can be used as an argument in fsetpos() function, and not meant to be read directly.

In the <stdio.h> header file, it is defined as follows:

typedef /* implementation-defined */ fpos_t;            

Example:

The example below shows the usage of fpos_t type. The program creates an empty file for output operations if the file test.txt does not exist. If the file already exists, its contents are discarded and the file is treated as a new empty file.

First, initial position of the file is get using fgetpos() function and then "Hello World!." is written to it. Then the position of the file is set to start using fsetpos() function to display the content of the file.

#include <stdio.h>
 
int main (){

  FILE * pFile = fopen("test.txt","w+");
  fpos_t position;
  int c;

  //get the beginning position
  fgetpos(pFile, &position);
  fputs("Hello World!.", pFile);

  //set the position to the start
  fsetpos(pFile, &position);

  //display the content of the file
  c = getc(pFile);
  while(c != EOF) {
    putchar(c);
    c = getc(pFile);
  }

  //close the file
  fclose (pFile);

  return 0;
}

The output of the above code will be:

Hello World!.

❮ C <stdio.h> Library

5