Home Tutorial C C file fsetpos() function

 
 

C file fsetpos() function
Posted on: December 24, 2009 at 12:00 AM
This section demonstrates the use of fsetpos() function in C.

C file fsetpos() function

This section demonstrates the use of fsetpos() function in C. The function fsetpos() is used to set the position of the stream to the location indicated by the information in pos. This information is obtained from the function fgetpos().

Its syntax is: fsetpos(File *stream,const fpos_t *pos)

This  function sets the file position and state indicators for the stream pointed to by stream according to the value of the object pointed to by pos, where a value is obtained from the function fgetpos() on the same stream. In the following code, the function fopen(file, "w") open a file and allow to perform write operations. The fputs() function writes the string "That is an example of fsetpos function"  into the file. Then, we have used the fsetpos() function that sets the file position for the stream which allows the fputs() function to replace the string "That" with "This".

Here is the code:

#include <stdio.h>

int main() {
      FILE * pFile;
      fpos_t position;
      pFile = fopen("myfile.txt", "w");
      fgetpos(pFile, &position);
      fputs("That is an example of fsetpos() function", pFile);
      fsetpos(pFile, &position);
      fputs("This", pFile);
      fclose(pFile);
      return 0;
}


Related Tags for C file fsetpos() function:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.