fseek() — fseeko() — Reposition File Position

Format

#include <stdio.h>
int fseek(FILE *stream, long int offset, int origin);
int fseeko(FILE *stream, off_t offset, int origin);

Language Level: ANSI

Threadsafe: Yes.

Integrated File System Interface: The fseeko() function is not available when SYSIFCOPT(*NOIFSIO) is specified on the compilation command.

Description

The fseek() and fseeko() functions change the current file position that is associated with stream to a new location within the file. The next operation on stream takes place at the new location. On a stream open for update, the next operation can be either a reading or a writing operation.

The fseeko() function is identical to fseek() except that the offset argument is of type off_t.

The origin must be one of the following constants that are defined in <stdio.h>:

Origin
Definition
SEEK_SET
Beginning of file
SEEK_CUR
Current position of file pointer
SEEK_END
End of file

For a binary stream, you can also change the position beyond the end of the file. An attempt to position before the beginning of the file causes an error. If successful, the fseek() or fseeko() function clears the end-of-file indicator, even when origin is SEEK_END, and undoes the effect of any preceding the ungetc() function on the same stream.

Note:
For streams opened in text mode, the fseek() and fseeko() functions have limited use because some system translations (such as those between carriage-return-line-feed and new line) can produce unexpected results. The only fseek() and fseeko() operations that can be relied upon to work on streams opened in text mode are seeking with an offset of zero relative to any of the origin values, or seeking from the beginning of the file with an offset value returned from a call to the ftell()or ftello() functions. Calls to the ftell() and ftello() functions are subject to their restrictions.

Return Value

The fseek() or fseeko function returns 0 if it successfully moves the pointer. A nonzero return value indicates an error. On devices that cannot seek, such as terminals and printers, the return value is nonzero.

The value of errno can be set to:

Value
Meaning
EBADF
The file pointer or descriptor is invalid.
EBADSEEK
Bad offset for a seek operation.
ENODEV
Operation was attempted on a wrong device.
ENOTOPEN
The file is not open.
ERECIO
The file is open for record I/O.
ESTDERR
stderr cannot be opened.
ESTDIN
stdin cannot be opened.
ESTDOUT
stdout cannot be opened.
EIOERROR
A non-recoverable I/O error occurred.
EIORECERR
A recoverable I/O error occurred.

The fseek() and fseeko() functions are not supported for files that are opened with type=record.

Example that uses fseek()

This example opens a file myfile for reading. After performing input operations,fseek() moves the file pointer to the beginning of the file.

#include <stdio.h>
#define  MAX_LEN  10
 
int main(void)
{
   FILE *stream;
   char buffer[MAX_LEN + 1];
   int  result;
   int  i;
   char ch;
 
   stream = fopen("mylib/myfile", "r");
   for (i = 0; (i  < (sizeof(buffer)-1) &&
       ((ch = fgetc(stream)) != EOF) && (ch != '\n')); i++)
          buffer[i] = ch;
 
   result = fseek(stream, 0L, SEEK_SET);  /* moves the pointer to the */
                                          /* beginning of the file    */
   if (result == 0)
      printf("Pointer successfully moved to the beginning of the file.\n");
   else
      printf("Failed moving pointer to the beginning of the file.\n");
}

Related Information



[ Top of Page | Previous Page | Next Page | Contents | Index ]