請多多利用
MSDNMoves the file pointer to a specified location.
int fseek(FILE *stream, long offset, int origin);Parameters stream
Pointer to FILE structure.
offset
Number of bytes from origin.
origin
Initial position.
Return Value If successful, fseek returns 0. Otherwise, it returns a nonzero value. On devices incapable of seeking, the return value is undefined.
Example複製程式
// crt_fseek.c
/* This program opens the file FSEEK.OUT and
* moves the pointer to the file's beginning.
*/
#include <stdio.h>
int main( void )
{
FILE *stream;
char line[81];
int result;
stream = fopen( "fseek.out", "w+" );
if( stream == NULL )
printf( "The file fseek.out was not opened\n" );
else
{
fprintf( stream, "The fseek begins here: "
"This is the file 'fseek.out'.\n" );
result = fseek( stream, 23L, SEEK_SET);
if( result )
perror( "Fseek failed" );
else
{
printf( "File pointer is set to middle of first line.\n" );
fgets( line, 80, stream );
printf( "%s", line );
}
fclose( stream );
}
}
Output複製程式
File pointer is set to middle of first line.
This is the file 'fseek.out'.