cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : C Library : cstdio (stdio.h) : fgetpos
 
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
C Library
cassert (assert.h)
cctype (ctype.h)
cerrno (errno.h)
cfloat (float.h)
climits (limits.h)
clocale (locale.h)
cmath (math.h)
csetjmp (setjmp.h)
csignal (signal.h)
cstdarg (stdarg.h)
cstddef (stddef.h)
cstdio (stdio.h)
cstdlib (stdlib.h)
cstring (string.h)
ctime (time.h)
cstdio (stdio.h)
functions:
· clearerr
· fclose
· feof
· ferror
· fflush
· fgetc
· fgetpos
· fgets
· fopen
· fprintf
· fputc
· fputs
· fread
· freopen
· fscanf
· fseek
· fsetpos
· ftell
· fwrite
· getc
· getchar
· gets
· perror
· printf
· putc
· putchar
· puts
· remove
· rename
· rewind
· scanf
· setbuf
· setvbuf
· sprintf
· sscanf
· tmpfile
· tmpnam
· ungetc
· vfprintf
· vprintf
· vsprintf
macro constants:
· EOF
· FILENAME_MAX
· NULL
· TMP_MAX
objects:
· stderr
· stdin
· stdout
types:
· FILE
· fpos_t
· size_t

-

fgetpos function
int fgetpos ( FILE * stream, fpos_t * position );
<cstdio>

Get current position in stream

Gets the information needed to uniquely identify the current value of the stream's position indicator and stores it in the location pointed by position.
The parameter position should point to an already allocated object of the type fpos_t, which is only intended to be used as a paremeter in future calls to fsetpos.
To retrieve the value of the internal file position indicator as an integer value, use ftell function instead.

Parameters

stream
Pointer to a FILE object that identifies the stream.
position
Pointer to a fpos_t object.

Return Value

The function return a zero value on success, and a non-zero value in case of error.

Example

/* fgetpos example */
#include <stdio.h>
int main ()
{
   FILE * pFile;
   int c;
   int n;
   fpos_t pos;

   pFile = fopen ("myfile.txt","r");
   if (pFile==NULL) perror ("Error opening file");
   else
   {
     c = fgetc (pFile);
     printf ("1st character is %c\n",c);
     fgetpos (pFile,&pos);
     for (n=0;n<3;n++)
     {
        fsetpos (pFile,&pos);
        c = fgetc (pFile);
        printf ("2nd character is %c\n",c);
     }
     fclose (pFile);
   }
   return 0;
}
Output:
1st character is A
2nd character is B
2nd character is B
2nd character is B

The example opens myfile.txt, reads the first character once and then it reads 3 times the same second character.

See also

fsetpos Set position indicator of stream (function)
ftell Get current position in stream (function)
fseek Reposition stream position indicator (function)
© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us