gtpc2m3bC/C++ Language Support User's Guide

fseek-Change File Position

This function sets a file position.

Format

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

stream
The stream for which the current position will be set.

offset
The distance in bytes from the origin.

origin
A position in the stream.

The origin must be one of the following constants defined in the stdio.h header file:

Origin
Definition

SEEK_SET
Beginning of the file.

SEEK_CUR
Current position of the file pointer.

SEEK_END
End of file.

This function changes the current file position associated with stream to a new location in the file. The next operation on the 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.

Note:
If you specify SEEK_CUR, any characters that were pushed back by the ungetc or ungetwc functions will have backed up the current position of the file pointer, which is the starting point of the seek. The seek will discard any pushed-back characters before repositioning, but the starting point will still be affected. For more information about calling the fseek after an ungetc function see ungetc-Push Character to Input Stream. You can calculate your own offsets. If the offset exceeds the end-of-file (EOF) and you subsequently write new data, your file is extended with nulls.

Attempting to reposition before the start of the file causes the fseek function to fail.

Attention: Repositioning in a wide-oriented file and performing updates is not recommended because it is not possible to predict if your update will overwrite part of a multibyte string or character, thereby invalidating subsequent data. For example, you could inadvertently add data that overwrites a shift-out. The following data expects the shift-out to be there, so it is not valid if it is treated as if in the initial shift state. Repositioning to the end of the file and adding new data is safe.

If successful, the fseek function clears the EOF indicator even when origin is SEEK_END and cancels the effect of any preceding ungetc or ungetwc function on the same stream.

If the call to the fseek function or the fsetpos function is not valid, the call is treated as a flush and the ungetc characters are discarded.

Normal Return

If it successfully moves the pointer, the fseek function returns a zero value.

Error Return

A nonzero return value indicates an error. On devices that cannot seek, such as terminals and printers, the return value is nonzero.

Programming Considerations

None.

Examples

The following example opens a myfile.dat file for reading. After performing input operations (not shown), the file pointer is moved to the beginning of the file.

#include <stdio.h>
 
int main(void)
{
   FILE *stream;
   int result;
 
   if (stream = fopen("myfile.dat", "r"))
   { /* successful */
 
   if (fseek(stream, 0L, SEEK_SET));  /* moves pointer to   */
                                  /* the beginning of the file */
   { /* if not equal to 0
             then error ...    */
   }
   else {
       /* fseek() successful  */
   }
}

Related Information

See Appendix E, Programming Support for the TPF File System for more information about TPF File System C Functions.