Read and Write Binary Data

DESCRIPTION:

Read or write binary data from a connection.

USAGE:

readBin(con, what, n=1, size=NA, signed=TRUE, endian=.Platform$endian, output=NULL)
writeBin(object, con, size=NA, endian=.Platform$endian)

ARGUMENTS:

con
a connection object or a character string naming a file.
object
an atomic object to be written to con.
what
a character string denoting the mode of the data to be read, one of: '"numeric", "double", "integer", "int", "logical", "complex", "character"'. Alternatively, what can be object whose mode will be used for the mode of the object to be read.
n
an integer specifying the maximum number of values to be read.
size
an integer specifying the number of bytes per value read. The default of NA uses a default size for a value of the given mode (4 for integer and single, 8 for double, and 16 for complex, the same on both 32 and 64 bit versions of S-PLUS). When reading character data this is ignored: character data is read as null-terminated strings of single byte characters.
signed
a logical value; if TRUE, integers are regarded as signed integers.
endian
a character string, either '"big"' or '"little"', specifying the endian-ness of the values read or written. If endian="swap" the endian-ness will be swapped. Intel hardware is little endian and Sun hardware is big endian.
output
Like what but used when the type of data in the file is different than the type of S-PLUS vector used to store the data. what refers to the data in the file and output refers to the output of readBin. output=double() is useful for reading unsigned 4 byte integers (so integers bigger than 2^31 are not read as negative numbers) or even for 8 byte integers on 32-bit versions of Splus. In the latter case you may lose some precision, but reading it as 4 byte integers will omit the 4 high order bytes.

DETAILS:

If con is a character string, the function will create a file connection to a file of that name.

VALUE:

For readBin, a vector of the appropriate mode. The length is the number of items read.

For writeBin, NULL.

NOTE:

These functions are modeled after the functions of the same name in R.

SEE ALSO:

, , file, scan, .Platform.

EXAMPLES:

tf <- tempfile()
x <- as.integer(c(-1,1)*3^(0:19))
writeBin(con=tf, x)
readBin(tf, integer(), n=20)
unlink(tf)