fseek(), fseeko(), fseeko64()

Change the current position of a stream

Synopsis:

#include <stdio.h>

int fseek( FILE *fp, 
           long offset, 
           int whence );

int fseeko( FILE *fp, 
            off_t offset, 
            int whence );

int fseeko64( FILE *fp,
              off64_t offset,
              int whence );

Arguments:

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.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The fseek(), fseeko(), and fseeko64() functions change 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 difference between these functions is the data type of the offset argument. The fseeko64() function is a large-file support version of fseeko().

Note: In QNX Neutrino 6.6 or later, the large-file support functions and data types appear in the name space only if you define _LARGEFILE64_SOURCE when you compile your code. For more information, see Classification in What's in a Function Description?

These functions clear the end-of-file indicator, and undo any effects of the ungetc() function on the stream.

You can use ftell(), ftello(), or ftello64() to get the current position of the stream before changing it. You can restore the position by using the value returned by one of the ftell() functions in a subsequent call to the corresponding fseek() function with the whence parameter set to SEEK_SET.

Returns:

0
Success.
-1
An error occurred; errno is set.

Errors:

These functions fail if, either the stream is unbuffered or the stream's buffer needed to be flushed, and the call to fseek(), fseeko(), or fseeko64() causes an underlying lseek() or write() to be invoked, and:

EAGAIN
The O_NONBLOCK flag is set for the file descriptor, and the process would be delayed in the write operation.
EBADF
The file descriptor underlying the stream file isn't open for writing, or the stream's buffer needed to be flushed and the file isn't open.
EFBIG
One of the following:
  • An attempt was made to write a file that exceeds the maximum file size.
  • The file is a regular file, and an attempt was made to write at or beyond the offset maximum associated with the corresponding stream.
EINTR
The write operation was terminated due to the receipt of a signal; no data was transferred.
EINVAL
The whence argument is invalid. The resulting file-position indicator would be set to a negative value.
EIO
One of the following:
  • A physical I/O error has occurred.
  • The process is a member of a background process group attempting to perform a write() to its controlling terminal, TOSTOP is set, the process is neither ignoring nor blocking SIGTTOU, and the process group of the process is orphaned.
  • (QNX Neutrino extension) The filesystem resides on a removable media device, and the media has been forcibly removed.
ENOSPC
There was no free space remaining on the device containing the file.
ENOSYS
The underlying device is incapable of seeking.
ENXIO
A request was made of a nonexistent device, or the request was outside the capabilities of the device.
EOVERFLOW
  • For fseek(), the resulting file offset would be a value that can't be represented correctly in an object of type long.
  • For fseeko(), the resulting file offset would be a value that can't be represented correctly in an object of type off_t.
EPIPE
An attempt was made to write to a pipe or FIFO that isn't open for reading by any process; a SIGPIPE signal is also sent to the thread.
ESPIPE
The file descriptor underlying the stream is associated with a pipe, FIFO, or socket.

Examples:

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;
}

Classification:

fseek() is ANSI, POSIX 1003.1; fseeko() is POSIX 1003.1; fseeko64() is Large-file support

Safety:  
Cancellation point Yes
Interrupt handler No
Signal handler Yes
Thread Yes