Gets a stream's current file position indicator value.
#include <stdio.h> int fgetpos(FILE *stream, fpos_t *pos);
stream
A pointer to a file stream.
pos
A pointer to a file position.
The fgetpos() function is used in conjunction with the fsetpos() function to allow random access to a file. The fgetpos() function gives unreliable results when used with streams associated with a console ( stdin, stderr, stdout).
While the fseek() and ftell() functions use long integers to read and set the file position indicator, fgetpos() and fsetpos() use fpos_t values to operate on larger files. The fpos_t type, defined in stdio.h, can hold file position indicator values that do not fit in a long int.
The fgetpos() function stores the current value of the file position indicator for stream in the fpos_t variable pos points to.
This facility may have limited capability on configurations of the EWL that run on platforms that do not have console input/output or a file system.
fgetpos() returns zero when successful and returns a nonzero value when it fails.
#include <stdio.h> #include <stdlib.h> int main(void) { FILE *f; fpos_t pos; char filename[80], buf[80]; // get a filename from the user printf("Enter a filename to read.\n"); gets(filename); // open the file for input if (( f = fopen(filename, "r")) == NULL) { printf("Can't open %s.\n", filename); exit(1); } printf("Reading each line twice.\n"); // get the initial file position indicator value // (which is at the beginning of the file) fgetpos(f, &pos); // read each line until end-of-file is reached while (fgets(buf, 80, f) != NULL) { printf("Once: %s", buf); // move to the beginning of the line to read it again fsetpos(f, &pos); fgets(buf, 80, f); printf("Twice: %s", buf); // get the file position of the next line fgetpos(f, &pos); } // close the file fclose(f); return 0; } Output: Enter a filename to read. myfoo Reading each line twice. Once: chair table chest Twice: chair table chest Once: desk raccoon Twice: desk raccoon