Functions to Reposition Connections
Functions to re-position connections.
seek(con, ...) "seek"(con, where = NA, origin = "start", rw = "", ...)isSeekable(con)truncate(con, ...)
- a connection.
- numeric. A file position (relative to the origin
- character string. Empty or
"write", partial matches allowed.
- character string. One of
"end": see Details.
- further arguments passed to or from other methods.
where = NA returns the current byte offset
of a connection (from the beginning), and with a non-missing
argument the connection is re-positioned (if possible) to the
isSeekable returns whether the connection
in principle supports
seek: currently only (possibly
gz-compressed) file connections do.
where is stored as a real but should represent an integer:
non-integer values are likely to be truncated. Note that the possible
values can exceed the largest representable number in an R
integer on 64-bit builds, and on some 32-bit builds.
File connections can be open for both writing/appending, in which case
R keeps separate positions for reading and writing. Which
refers to can be set by its
rw argument: the default is the
last mode (reading or writing) which was used. Most files are
only opened for reading or writing and so default to that state. If a
file is open for both reading and writing but has not been used, the
default is to give the reading position (0).
The initial file position for reading is always at the beginning.
The initial position for writing is at the beginning of the file
"r+b", otherwise at the end of the
file. Some platforms only allow writing at the end of the file in
the append modes. (The reported write position for a file opened in
an append mode will typically be unreliable until the file has been
gzfile connections support
seek with a number of
limitations, using the file position of the uncompressed file.
They do not support
origin = "end". When writing, seeking is
only possible forwards: when reading seeking backwards is supported by
rewinding the file and re-reading from its start.
seek is called with a non-
NA value of
any pushback on a text-mode connection is discarded.
truncate truncates a file opened for writing at its current
position. It works only for
file connections, and is not
implemented on all platforms: on others (including Windows) it will
not work for large (> 2Gb) files.
None of these should be expected to work on text-mode connections with re-encoding selected.
seekreturns the current position (before any move), as a (numeric) byte offset from the origin, if relevant, or
0if not. Note that the position can exceed the largest representable number in an R
integeron 64-bit builds, and on some 32-bit builds.
NULL: it stops with an error if it fails (or is not implemented).
isSeekablereturns a logical value, whether the connection supports
seek on Windows is discouraged. We have found so many
errors in the Windows implementation of file positioning that users
are advised to use it only at their own risk, and asked not to waste
the R developers' time with bug reports on Windows' deficiencies.