Stores the current file position for a given file.
Format
#include <stdio.h>
int fgetpos (FILE *stream, fpos_t *pos);
1 – Arguments
stream
A file pointer.
pos
A pointer to an implementation-defined structure. The fgetpos
function fills this structure with information that can be used
on subsequent calls to fsetpos.
2 – Description
The fgetpos function stores the current value of the file
position indicator for the stream pointed to by stream into the
object pointed to by pos.
3 – Return Values
0 Indicates successful completion.
-1 Indicates that there are errors.
4 – Example
#include <stdio.h>
#include <stdlib.h>
main()
{
FILE *fp;
int stat,
i;
int character;
char ch,
c_ptr[130],
d_ptr[130];
fpos_t posit;
/* Open a file for writing. */
if ((fp = fopen("file.dat", "w+")) == NULL) {
perror("open");
exit(1);
}
/* Get the beginning position in the file. */
if (fgetpos(fp, &posit) != 0)
perror("fgetpos");
/* Write some data to the file. */
if (fprintf(fp, "this is a test\n") == 0) {
perror("fprintf");
exit(1);
}
/* Set the file position back to the beginning. */
if (fsetpos(fp, &posit) != 0)
perror("fsetpos");
fgets(c_ptr, 130, fp);
puts(c_ptr); /* Should be "this is a test." */
/* Close the file. */
if (fclose(fp) != 0) {
perror("close");
exit(1);
}
}