Usage

Arguments

con

A connection object or a character string naming a file or
a raw vector.

what

Either an object whose mode will give the mode of the
vector to be read, or a character vector of length one describing
the mode: one of "numeric", "double",
"integer", "int", "logical", "complex",
"character", "raw".

n

integer. The (maximal) number of records to be
read. You can use an over-estimate here, but not too large as
storage is reserved for n items.

size

integer. The number of bytes per element in the byte
stream. The default, NA_integer_, uses the natural size.
Size changing is not supported for raw and complex vectors.

signed

logical. Only used for integers of sizes 1 and 2,
when it determines if the quantity on file
should be regarded as a signed or unsigned integer.

endian

The endian-ness ("big" or "little") of the
target system for the file. Using "swap" will force swapping
endian-ness.

Details

These functions are intended to be used with binary-mode connections.
If con is a character string, the functions call
file to obtain a binary-mode file connection which is
opened for the duration of the function call.

If the connection is open it is read/written from its current
position. If it is not open, it is opened for the duration of the
call in an appropriate mode (binary read or write) and then closed
again. An open connection must be in binary mode.

If readBin is called with con a raw vector, the data in
the vector is used as input. If writeBin is called with
con a raw vector, it is just an indication that a raw vector
should be returned.

If size is specified and not the natural size of the object,
each element of the vector is coerced to an appropriate type before
being written or as it is read. Possible sizes are 1, 2, 4 and
possibly 8 for integer or logical vectors, and 4, 8 and possibly 12/16
for numeric vectors. (Note that coercion occurs as signed types
except if signed = FALSE when reading integers of sizes 1 and 2.)
Changing sizes is unlikely to preserve NAs, and the extended
precision sizes are unlikely to be portable across platforms.

readBin and writeBin read and write C-style
zero-terminated character strings. Input strings are limited to 10000
characters. readChar and writeChar can
be used to read and write fixed-length strings. No check is made that
the string is valid in the current locale's encoding.

Handling R's missing and special (Inf, -Inf and
NaN) values is discussed in the ‘R Data Import/Export’ manual.

Only 2^31 - 1 bytes can be written in a single
call (and that is the maximum capacity of a raw vector on 32-bit
platforms).

‘Endian-ness’ is relevant for size > 1, and should
always be set for portable code (the default is only appropriate when
writing and then reading files on the same platform).

Value

For readBin, a vector of appropriate mode and length the number of
items read (which might be less than n).

For writeBin, a raw vector (if con is a raw vector) or
invisibly NULL.

Note

Integer read/writes of size 8 will be available if either C type
long is of size 8 bytes or C type long long exists and
is of size 8 bytes.

Real read/writes of size sizeof(long double) (usually 12 or 16
bytes) will be available only if that type is available and different
from double.

If readBin(what = character()) is used incorrectly on a file
which does not contain C-style character strings, warnings (usually
many) are given. From a file or connection, the input will be broken
into pieces of length 10000 with any final part being discarded.

Using these functions on a text-mode connection may work but should
not be mixed with text-mode access to the connection, especially if
the connection was opened with an encoding argument.