Repositioning within files

You can use the following library functions to help you position within a file:
With large file support enabled for AMODE 31 C/C++ applications, you can use the following library functions for 64-bit offsets: For AMODE 64 C/C++ applications, large files are automatically supported in the LP64 programming model. All of the above functions (both lists) can be used with 64-bit offsets.

See z/OS XL C/C++ Runtime Library Reference for more information on these library functions.

Opening a file with fopen() and specifying the NOSEEK parameter disables all of these library functions except rewind(). A call to rewind() causes the file to be reopened, unless the file is a non-disk file opened for write-only. In this case, rewind() sets errno and raises SIGIOERR (if SIGIOERR is not set to SIG_IGN, which is its default).

Calling any of these functions flushes all complete and updated records out to the system. If a repositioning operation fails, z/OS® XL C/C++ attempts to restore the original file position and treats the operation as a call to fflush(), except that it does not account for the presence of ungetc() or ungetwc() characters, which are lost. After a successful repositioning operation, feof() always returns 0, even if the position is just after the last byte of data in the file.

The fsetpos() and fgetpos() library functions are generally more efficient than ftell() and fseek(). The fgetpos() function can encode the current position into a structure that provides enough room to hold the system position as well as position data specific to C or C++. The ftell() function must encode the position into a single word of storage, which it returns. This compaction forces fseek() to calculate certain position information specific to C or C++ at the time of repositioning. For variable-format binary files, you can choose to have ftell() return relative byte offsets. In previous releases, ftell() returned only encoded offsets, which contained the relative block number. Since you cannot calculate the block number from a relative byte offset in a variable-format file, fseek() may have to read through the file to get to the new position. fsetpos() has system position information available within the the fpos_t structure and can generally reposition directly to the desired location.

You can use the ftell() and fseek() functions to set the current position within all types of files except for the following:

Although repositioning within files opened for write mode is not available, you can use fgetpos() and ftell() to save the current position, and this position can later be used to reposition within the same file if opened in one of the modes where reposition is allowed.

For AMODE 31 C/C++ applications, the repositioning functions can be used with large format sequential data sets under the following conditions: