Usage of the fseek Function
Function Name: fseek
Function: relocate the file pointer on the stream
Usage: int fseek (File * stream, long offset, int fromwhere );
Program example:
# Include <stdio. h>
Long filesize (File * stream );
Int main (void)
{
File * stream;
Stream = fopen ("myfile. txt", "W + ");
Fprintf (stream, "this is a test ");
Printf ("filesize of myfile. txt is % LD Bytes/N", filesize (Stream ));
Fclose (Stream );
Return 0;
}
Long filesize (File * Stream)
{
Long curpos, length;
Curpos = ftell (Stream );
Fseek (stream, 0l, seek_end );
Length = ftell (Stream );
Fseek (stream, curpos, seek_set );
Return length;
}
========================================================== ======================================
Fseek () function usage
Call form:
# Include "stdio. H"
Fseek (file type pointer FP, displacement, starting point );
|
Function: Place the FP-Related File Location pointer to a specified location.
Where,"Displacement"Is long data, which indicates the number of bytes that the position pointer moves relative to the" starting point. If the displacement is a positive value, it indicates moving from the start point to the end of the file. If the displacement is a negative value, it indicates moving from the start point to the end of the file.
"Start PointIt can only be one of the three symbol constants defined in stdio. h:
Start Point |
Corresponding number |
File Location |
Seek_set |
0 |
File start |
Seek_cur |
1 |
Current File Location |
Seek_end |
2 |
End of File |
For example:
Fseek (FP, 50l, 0); or fseek (FP, 50l, seek_set );
It moves the position pointer to 50 bytes away from the file header.
Note: After fseek (FP, 0l, seek_end), the current pointer of the file will be the last position, so if you need to read it, it will be read from there