fsetpos() prototype
int fsetpos(FILE* stream, const fpos_t* pos);
The fsetpos()
function takes a file stream and a pointer to an fpos_t object obtained from a call to fgetpos() as its argument.
- A call to
fsetpos()
function cancels the effects ofungetc
and clears the end-of-file state, if it is set. - Incase of any read or write error, the error indicator i.e.
ferror
for the stream is set.
It is defined in <cstdio> header file.
fsetpos() Parameters
- stream: The file stream whose position is to be set.
- pos: Position value obtained from a previous call to fgetpos that indicates the position of the file pointer at that moment.
fsetpos() Return value
On success the fsetpos()
function returns zero, nonzero otherwise.
Example: How fsetpos() function works
#include <cstdio>
int main()
{
FILE *fp;
fpos_t pos;
int c;
fp = fopen("myfile.txt","w+");
fputs("What a boring day!\n",fp);
fgetpos(fp, &pos);
fputs("The weather is bad",fp);
fsetpos(fp, &pos);
/* Replaces the second line by new string */
fputs("It is raining badly.",fp);
rewind(fp);
while(!feof(fp))
{
c = getc(fp);
putchar(c);
}
fclose(fp);
return 0;
}
When you run the program, the output will be:
What a boring day! It is raining badly.