Function name : fseek
Header file : <stdio.h>
Function prototype : int fseek(FILE *stream, long offset, int fromwhere);
Function: Relocate the file pointer on the stream to relocate the position of the file structure on the stream. fseek sets the file pointer associated with the stream stream to a new position offset bytes from the file position given by fromwhere.
Parameters : FILE *stream the stream to be relocated
long offset offset of relocation
int fromwhere the location of the relocation
Return value : 0 on success, non-0 on error or failure.
Supplement : The value of fromwhere must be 0, one of 1 or 2 respectively represents the three symbolic constants defined in stdio.h:
0 is SEEK_SET, which is the starting position of the file;
1 is SEEK_CUR, which is the current pointer position;
At 2 o'clock SEEK_END is the end of the file.
After fseek is called, the next operation at the updated file location can be input; it can also be output.
Program example: Open the file, input the string into the file stream, relocate it to the beginning of the file, and output the length of the file
#include<stdio.h>longfilesize(FILE*stream);intmain(void){FILE*stream=fopen(myfile.txt,w+);fprintf(stream,www.dotcpp.com);fseek(stream,0,SEEK_END );printf(Filesizeofmyfile.txtis%ldbytesn,ftell(stream));fclose(stream);return0;}
Running results
Filesizeofmyfile.txtis14bytes