\(\text{FileReadInteger}\)¶
You can use the the \(\text{FileReadInteger}\) function to read a single integer value.
You can use the \filereadinteger
backslash command to insert this function.
The following variants of this function are available:
\(\text{tuple } \text{FileReadInteger} \left ( \text{<file number>} \right )\)
\(\text{tuple } \text{FileReadInteger} \left ( \text{<file number>}, \text{<size/endian>} \right )\)
The \(\text{<file number>}\) parameter is an integer value returned by one of the \(\text{FileOpen}\ldots\) functions. The \(\text{<size/endian>}\) parameter indicates how the integer value to be read is encoded in the file.
If the \(\text{<size/endian>}\) value is not included or is zero, then the
\(\text{FileReadInteger}\) function assumes that the integer is encoded
as a string representing the integer value. The integer value can include
an initial -
character to indicate a negative value or can be prepended
with 0x
or 0b
to indicate an unsigned base-16 or base-2 value. In
this mode, the \(\text{FileReadInteger}\) function will read up-to but
excluding the first non-digit character.
If the \(\text{<size/endian>}\) value is greater than zero, then the \(\text{FileReadInteger}\) function assumes that the integer is in a little-endian signed binary format. In this mode, the integer value will be interpreted as
where \(r\) is the integer result, \(b\) represents the file bytes as an array of values, \(p\) represents the current file position, and \(N\) represents the \(\text{<size/endian>}\) value supplied to the function.
If the \(\text{<size/endian>}\) value is less than zero, then the \(\text{FileReadInteger}\) function assumes that the integer is in a big-endian signed binary format. In this mode, the integer value will be interpreted as
The maximum size integer that can be read is 8 bytes. Specifying a \(\text{<size/endian>}\) value with a magnitude greater than 8 will cause only 8 bytes to be read in little-endian or big-endian format.
The \(\text{FileReadInteger}\) function will return a tuple containing two entries:
The integer value read from the file.
A boolean holding
true
on success orfalse
on error.
The Figure 136 shows how you can use the \(\text{FileReadInteger}\) function.