C++ Standard Library C++ STL Library

C++ <cstdio> - fpos_t Type



The C++ <cstdio> 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 <cstdio> 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 <cstdio>
 
int main (){
  //open the file in write and read mode
  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++ <cstdio> Library