ftello/fseeko vs fgetpos/fsetpos
Asked Answered
R

1

14

What is the difference between ftello/fseeko and fgetpos/fsetpos? Both seem to be file pointer getting/setting functions that use opaque offset types to sometimes allow 64 bit offsets.

Are they supported on different platforms or by different standards? Is one more flexible in the type of the offset it uses?

And, by the way, I am aware of what is difference between fgetpos/fsetpos and ftell/fseek, but this is not a duplicate. That question asks about ftell/fseek, and the answer is not applicable to ftello/fseeko.

Redfish answered 25/8, 2012 at 4:23 Comment(3)
Note that the off_t returned by ftello isn't actually an opaque data type; it is just a signed integer. It might be 32-bit or 64-bit depending on your platform and compilation flags, though. Similarly, fpos_t might not be 64-bit if _FILE_OFFSET_BITS != 64.Tanh
I guess what I meant by an opaque data type is that it is not of a fixed type in the user's code. I guess it's not quite opaque, because the data type can only be changed at compile time, not at dynamic link time.Redfish
Ah. I usually use opaque in the sense of 'structure with nonpublic layout', like fpos_t is.Tanh
T
12

See Portable Positioning for detailed information on the difference. An excerpt:

On some systems where text streams truly differ from binary streams, it is impossible to represent the file position of a text stream as a count of characters from the beginning of the file. For example, the file position on some systems must encode both a record offset within the file, and a character offset within the record.

As a consequence, if you want your programs to be portable to these systems, you must observe certain rules:

  • The value returned from ftell on a text stream has no predictable relationship to the number of characters you have read so far. The only thing you can rely on is that you can use it subsequently as the offset argument to fseek or fseeko to move back to the same file position.
  • In a call to fseek or fseeko on a text stream, either the offset must be zero, or whence must be SEEK_SET and the offset must be the result of an earlier call to ftell on the same stream.
  • The value of the file position indicator of a text stream is undefined while there are characters that have been pushed back with ungetc that haven't been read or discarded. See Unreading.

In a nutshell: fgetpos/fsetpos use a more flexible structure to store additional metadata about the file position state, enabling greater portability (in theory).

Tanh answered 25/8, 2012 at 4:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.