FGETPOS(S) UNIX System V FGETPOS(S)
Name
fgetpos - gets and stores the current value of a stream's
file position indicator
Syntax
#include <stdio.h>
int fgetpos(stream, pos)
FILE *stream;
fpos_t *pos;
Description
The fgetpos function gets the current value of stream's file
position indicator and stores it in the object that pos
points to. The fsetpos function can later use information
stored in pos to reset stream's pointer to its position at
the time fgetpos was called.
Notes
The pos value is stored in an internal format and is
intended for use only by the fgetpos and fsetpos functions.
Return Value
If successful, the fgetpos function returns 0. On failure,
it returns a nonzero value and sets errno to one of the
following manifest constants (defined in stdio.h):
Constant Meaning
EINVAL The stream value is invalid.
EBADF The specified stream is not
a valid file handle or is
not accessible.
See Also
fsetpos(S)
Example
#include <stdio.h>
FILE *stream;
fpos_t pos[];
int val, i;
char fcontents[13]; /* buffer for full contents of file1 */
char writein1[14]= "Hello world!0;
char writein2[8]= "there.0;
main()
{
if ((stream = fopen("file1","w+")) == NULL) /* open file1 */
{
perror("fopen");
printf ("Trouble opening file\n");
}
else /* write in 1st string */
fwrite(writein1,sizeof(char),13,stream);
position=0;
fsetpos(stream, &position); /*reset file to beginning */
/* read contents */
fread(fcontents, sizeof(char), 13, stream);
/* and print it */
printf("%s", fcontents);
position=0;
fsetpos(stream, &position); /*reset file to beginning */
/* read "Hello" */
fread(fcontents, sizeof(char), 5, stream);
/* Save current position */
if (fgetpos(stream, &position) != 0)
perror("fgetpos error");
/* read some more */
fread(fcontents, sizeof(char), 8, stream);
/* Return to saved position */
if (fsetpos(stream, &position) != 0)
perror("fsetpos error");
/* Overwrite file from this position with the word "there" */
fwrite(writein2, sizeof(char), 8, stream);
position=0;
fsetpos(stream, &position); /*reset file to beginning */
fread(fcontents, sizeof(char), 13, stream);
printf("%s", fcontents); /* print contents */
}
}
This program opens a file named file1 and prints ``Hello
world!'' It then outputs the file contents on screen. Next,
it reads the first five characters in the file hello and
calls fgetpos to find and save the file position pointer.
After performing another read, the program calls fsetpos to
restore the file pointer to the saved position.
Standards Conformance
fgetpos is conformant with:
ANSI X3.159-198X C Language Draft Standard, May 13, 1988.
(printed 6/20/89)