gtpc2m3b | C/C++ Language Support User's Guide |
This function sets a file position.
Format
#include <stdio.h> int fseek(FILE *stream, long int offset, int origin);
The origin must be one of the following constants defined in the stdio.h header 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.
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.