C examples for stdio.h:fgetpos
function
<cstdio> <stdio.h>
int fgetpos ( FILE * stream, fpos_t * pos );
Get the current position in the stream.
Parameter | Description |
---|---|
stream | Pointer to a FILE object. |
pos | Pointer to a fpos_t object. |
On success, the function returns zero.
On error, errno is set to a positive value and the function returns a non-zero value.
The following code opens a file then reads the first character once, and then reads 3 times the same second character.
#include <stdio.h> int main ()//from w w w .ja v a2 s . c o m { FILE * pFile; int c; int n; fpos_t pos; pFile = fopen ("main.cpp","r"); if (pFile==NULL){ perror ("Error opening file"); return -1; } 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; }