Warning: main(/www/www/htdocs/style/globals.php) [function.main]: failed to open stream: No such file or directory in /www/www/docs/6.4.1/neutrino/lib_ref/f/fseek.html on line 1
Warning: main() [function.include]: Failed opening '/www/www/htdocs/style/globals.php' for inclusion (include_path='.:/www/www/common:/www/www/php/lib/php') in /www/www/docs/6.4.1/neutrino/lib_ref/f/fseek.html on line 1
Warning: main(/www/www/htdocs/style/header.php) [function.main]: failed to open stream: No such file or directory in /www/www/docs/6.4.1/neutrino/lib_ref/f/fseek.html on line 8
Warning: main() [function.include]: Failed opening '/www/www/htdocs/style/header.php' for inclusion (include_path='.:/www/www/common:/www/www/php/lib/php') in /www/www/docs/6.4.1/neutrino/lib_ref/f/fseek.html on line 8
Change the current position of a stream
#include <stdio.h>
int fseek( FILE* fp,
long offset,
int whence );
int fseeko( FILE* fp,
off_t offset,
int whence );
- fp
- A FILE pointer returned by
fopen()
or
freopen().
- offset
- The position to seek to, relative to one of the positions specified by
whence.
- whence
- The position from which to apply the offset; one of:
- SEEK_SET
- Compute the new file position relative to the start of the file.
The value of offset must not be negative.
- SEEK_CUR
- Compute the new file position relative to the current file
position. The value of offset may be positive, negative or
zero. A SEEK_CUR with a 0 offset
is necessary when you want to switch from reading to writing on
a stream opened for updates.
- SEEK_END
- Compute the new file position relative to the end of the file.
libc
Use the -l c option to
qcc
to link against this library.
This library is usually included automatically.
The fseek() function changes the current position of the stream
specified by fp.
This position defines the character that will be read or written by
the next I/O operation on the file.
The fseek() function clears the end-of-file indicator, and undoes
any effects of the
ungetc() function on the
stream.
You can use
ftell()
to get the current position of the stream before changing it.
You can restore the position by using the value returned by ftell()
in a
subsequent call to fseek() with the whence parameter
set to SEEK_SET.
0 for success, or nonzero if an error occurs.
If an error occurs, errno
is set to indicate the type of error.
Determine the size of a file, by saving and restoring the current position of
the file:
#include <stdio.h>
#include <stdlib.h>
long filesize( FILE *fp )
{
long int save_pos;
long size_of_file;
/* Save the current position. */
save_pos = ftell( fp );
/* Jump to the end of the file. */
fseek( fp, 0L, SEEK_END );
/* Get the end position. */
size_of_file = ftell( fp );
/* Jump back to the original position. */
fseek( fp, save_pos, SEEK_SET );
return( size_of_file );
}
int main( void )
{
FILE *fp;
fp = fopen( "file", "r" );
if( fp != NULL ) {
printf( "File size=%ld\n", filesize( fp ) );
fclose( fp );
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
fseek() is
ANSI,
POSIX 1003.1;
fseeko() is
POSIX 1003.1
Safety: | |
Cancellation point |
No |
Interrupt handler |
No |
Signal handler |
Yes |
Thread |
Yes |
errno,
fgetpos(),
fopen(),
fsetpos(),
ftell()
Warning: main(/www/www/htdocs/style/footer.php) [function.main]: failed to open stream: No such file or directory in /www/www/docs/6.4.1/neutrino/lib_ref/f/fseek.html on line 231
Warning: main() [function.include]: Failed opening '/www/www/htdocs/style/footer.php' for inclusion (include_path='.:/www/www/common:/www/www/php/lib/php') in /www/www/docs/6.4.1/neutrino/lib_ref/f/fseek.html on line 231