5.10. Positioning a StreamThere are three ways to position a standard I/O stream: The two functions ftell and fseek. They have been around since Version 7, but they assume that a file's position can be stored in a long integer. The two functions ftello and fseeko. They were introduced in the Single UNIX Specification to allow for file offsets that might not fit in a long integer. They replace the long integer with the off_t data type. The two functions fgetpos and fsetpos. They were introduced by ISO C. They use an abstract data type, fpos_t, that records a file's position. This data type can be made as big as necessary to record a file's position. Portable applications that need to move to non-UNIX systems should use fgetpos and fsetpos.
Section 3.6: SEEK_SET means from the beginning of the file, SEEK_CUR means from the current file position, and SEEK_END means from the end of file. ISO C doesn't require an implementation to support the SEEK_END specification for a binary file, as some systems require a binary file to be padded at the end with zeros to make the file size a multiple of some magic number. Under the UNIX System, however, SEEK_END is supported for binary files. For text files, the file's current position may not be measurable as a simple byte offset. Again, this is mainly under non-UNIX systems that might store text files in a different format. To position a text file, whence has to be SEEK_SET, and only two values for offset are allowed: 0meaning rewind the file to its beginningor a value that was returned by ftell for that file. A stream can also be set to the beginning of the file with the rewind function. The ftello function is the same as ftell, and the fseeko function is the same as fseek, except that the type of the offset is off_t instead of long.
Recall the discussion of the off_t data type in Section 3.6. Implementations can define the off_t type to be larger than 32 bits. As we mentioned, the fgetpos and fsetpos functions were introduced by the ISO C standard.
The fgetpos function stores the current value of the file's position indicator in the object pointed to by pos . This value can be used in a later call to fsetpos to reposition the stream to that location. |