The fsetpos() function moves the file position indicator of the given file stream to the specified position. When fsetpos() is executed, the end-of-file indicator is reset. fsetpos() function is a part of <cstdio> header file in C Standard Library.
Syntax of fsetpos()
int fsetpos(FILE *stream, const fpos_t *pos)
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 the desired value of the position indicator.
Return Value
- If it is successful, it returns zero otherwise returns a nonzero value.
C Program to Illustrate the use of fsetpos()
C
// C program to illustrate the use of fsetpos() #include <stdio.h> int main()
{ // File pointer
FILE * fp;
// Variable to store file position
fpos_t position;
// Open file in write and read mode
fp = fopen ( "myfile.txt" , "w+" );
// Get current file position and store it
fgetpos (fp, &position);
// Write "HelloWorld!" to the file
fputs ( "HelloWorld!" , fp);
// Set file position back to the stored position
fsetpos (fp, &position);
// Write "geeksforgeeks" to the file from the restored
// position
fputs ( "geeksforgeeks" , fp);
// setting file pointer position back to the stored
// position
fsetpos (fp, &position);
// storing data stored in file
char arr[50];
fgets (arr, 14, fp);
// printing output
puts (arr);
// Close the file
fclose (fp);
return (0);
} |
Output
geeksforgeeks
Article Tags :