Latest web development tutorials

C library functions - fsetpos ()

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

description

C library functionsint fsetpos (FILE * stream, const fpos_t * pos) is set to the file location given flow streamfor a given location.Pos parameter is given by the function fgetpos position.

statement

Here is () statement fsetpos function.

int fsetpos(FILE *stream, const 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 fpos_t object, the object contains the position before the adoption of fgetpos obtained.

return value

If successful, the function returns a zero value, otherwise it returns a non-zero value, and the global variableerrno to a positive value, this value can be explained by perror.

Examples

The following example demonstrates fsetpos () 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;

   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>