fsetpos() (Set File Position) in C

The fsetpos() function moves the file position indicator to the location specified by the object pointed to by position. When fsetpos() is executed ,the end-of-file indecator is reset.

Declaration


int fsetpos(FILE *stream, const fpos_t *position)

Parameters –

  • stream – This is the pointer to a FILE object that identifies the stream.
  • position – This is the pointer to a fpos_t object containing a position previously obtained with fgetpos.

Return – If it successful, it return zero otherwise returns nonzero value.

// c code to demonstrate fsetpos() function.
#include <stdio.h>
int main () {
   FILE *fp;
   fpos_t position;

   /*write your own file name.
    My file name is "myfile.txt"*/
   fp = fopen("myfile.txt","w+");
   fgetpos(fp, &position);
   fputs("HelloWorld!", fp);
  
   fsetpos(fp, &position);

   // previous function is override
   fputs("geeksforgeeks", fp);
   fclose(fp);
   
   return(0);
}

Output –

geeksforgeeks

This article is contributed by Shivani Ghughtyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.