Latest web development tutorials

C library functions - fgetpos ()

C standard library - <stdio.h> C standard library - <stdio.h>

description

C library functionsint fgetpos (FILE * stream, fpos_t * pos) Gets flow streamof the current file position, and write it topos.

statement

Here is () statement fgetpos function.

int fgetpos(FILE *stream, fpos_t *pos)

parameter

  • stream - This is a pointer to FILE pointer to an object, the object identifier FILE flow.
  • pos - This is a pointer to a pointer to an object fpos_t.

return value

If successful, the function returns zero. If an error occurs, it returns a nonzero value.

Examples

The following example demonstrates fgetpos () function is used.

#include <stdio.h>

int main ()
{
   FILE *fp;
   fpos_t position;

   fp = fopen("file.txt","w+");
   fgetpos(fp, &position);
   fputs("Hello, World!", fp);
  
   fsetpos(fp, &position);
   fputs("这将覆盖之前的内容", fp);
   fclose(fp);
   
   return(0);
}

Let's compile and run the above program, which will create a filefile.txt, it reads as follows.First, we usefgetpos () function to get the initial position of the file, and then we write to the fileHello, World,then we use fsetpos ()function to reset at the beginning of the write pointer to the file, the file is overwritten by the following text!:

这将覆盖之前的内容

Now let's use the following procedure to view the contents of the above file:

#include <stdio.h>

int main ()
{
   FILE *fp;
   int c;
   int n = 0;

   fp = fopen("file.txt","r");
   while(1)
   {
      c = fgetc(fp);
      if( feof(fp) )
      {
          break ;
      }
      printf("%c", c);
   }

   fclose(fp);

   return(0);
}

C standard library - <stdio.h> C standard library - <stdio.h>