fgetpos - C stdio.h

C examples for stdio.h:fgetpos

Type

function

From


<cstdio>
<stdio.h>

Prototype

int fgetpos ( FILE * stream, fpos_t * pos );

Description

Get the current position in the stream.

Parameters

ParameterDescription
stream Pointer to a FILE object.
pos Pointer to a fpos_t object.

Return Value

On success, the function returns zero.

On error, errno is set to a positive value and the function returns a non-zero value.

Example

The following code opens a file then reads the first character once, and then reads 3 times the same second character.

Demo Code


#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;
}

Related Tutorials