6.18.26 fsetpos Function

Sets the stream’s file position.

Attention: This function is not implemented by MPLAB XC8.

Include

<stdio.h>

Prototype

int fsetpos(FILE * stream, const fpos_t * pos);

Arguments

stream
target stream
pos
position-indicator storage as returned by an earlier call to fgetpos

Return Value

Returns 0 if successful; otherwise, returns a non-zero value.

Remarks

The function sets the file-position indicator for the given stream in *pos if successful; otherwise, fsetpos sets errno.

Example

/* This program opens a file and reads bytes at */
/* several different locations. The fgetpos     */
/* function notes the 8th byte. 21 bytes are   */
/* read then 18 bytes are read. Next, the        */
/* fsetpos function is set based on the         */
/* fgetpos position and the previous 21 bytes   */
/* are reread.                                  */

#include <stdio.h>

int main(void)
{
  FILE   * myfile;
  fpos_t pos;
  char   buf[25];
  if ((myfile = fopen("sampfgetpos.c", "rb")) == NULL)
    printf("Cannot open file\n");
  else
  {
    fread(buf, sizeof(char), 8, myfile);
    if (fgetpos(myfile, &pos) != 0)
      perror("fgetpos error");
    else
    {
      fread(buf, sizeof(char), 21, myfile);
      printf("Bytes read: %.21s\n", buf);
      fread(buf, sizeof(char), 18, myfile);
      printf("Bytes read: %.18s\n", buf);
    }
  if (fsetpos(myfile, &pos) != 0)
    perror("fsetpos error");

  fread(buf, sizeof(char), 21, myfile);
  printf("Bytes read: %.21s\n", buf);
  fclose(myfile);
  }
}

Example Output

Bytes read: program opens a file
Bytes read: and reads bytes at
Bytes read: program opens a file