On the GNU system, the file position is truly a character count. You
can specify any character count value as an argument to
get reliable results for any random access file. However, some ISO C
systems do not represent file positions in this way.
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:
ftellon 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
fseekto move back to the same file position.
fseekon a text stream, either the offset must either be zero; or whence must be
SEEK_SETand the offset must be the result of an earlier call to
ftellon the same stream.
ungetcthat haven't been read or discarded. See section Unreading.
But even if you observe these rules, you may still have trouble for long
fseek use a
long int value
to represent the file position. This type may not have room to encode
all the file positions in a large file.
So if you do want to support systems with peculiar encodings for the
file positions, it is better to use the functions
fsetpos instead. These functions represent the file position
using the data type
fpos_t, whose internal representation varies
from system to system.
These symbols are declared in the header file `stdio.h'.
In the GNU system,
fpos_t is equivalent to
long int. In other systems, it might have a different internal
fpos_tobject pointed to by position. If successful,
fgetposreturns zero; otherwise it returns a nonzero value and stores an implementation-defined positive value in
fgetposon the same stream. If successful,
fsetposclears the end-of-file indicator on the stream, discards any characters that were "pushed back" by the use of
ungetc, and returns a value of zero. Otherwise,
fsetposreturns a nonzero value and stores an implementation-defined positive value in
Go to the first, previous, next, last section, table of contents.