fgetpos() — Get File Position
Format
#include <stdio.h>
int fgetpos(FILE *stream, fpos_t *pos);
Language Level
ANSI
Threadsafe
Yes
Description
The fgetpos()
function
stores 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.
Return Value
The fgetpos()
function
returns 0 if successful; on error, it returns nonzero and
sets errno to a nonzero value.
The value of errno can be set to:
- Value
- Meaning
- EBADF
- The file pointer or descriptor is not valid.
- EBADSEEK
- Bad offset for a seek operation.
- ENODEV
- Operation was attempted on a wrong device.
- ENOTOPEN
- The file is not open.
- ERECIO
- The file is open for record I/O.
- ESTDERR
stderr
cannot be opened.- ESTDIN
stdin
cannot be opened.- ESTDOUT
stdout
cannot be opened.- EIOERROR
- A non-recoverable I/O error occurred.
- EIORECERR
- A recoverable I/O error occurred.
The fgetpos()
function
is not supported for files that are opened with type=record.
Example
This example opens the file myfile for
reading and stores the current file pointer position into the variable pos.
#include <stdio.h>
FILE *stream;
int main(void)
{
int retcode;
fpos_t pos;
stream = fopen("mylib/myfile", "rb");
/* The value returned by fgetpos can be used by fsetpos */
/* to set the file pointer if 'retcode' is 0 */
if ((retcode = fgetpos(stream, &pos) == 0)
printf("Current position of file pointer found\n");
fclose(stream);
}