readMapped(con, what, n = -1)
readRaw(con, what, n = -1)
writeRaw(object, con)
ARGUMENTS:
con
a connection. For
readMapped, this has to be a connection for which
memory mapping is defined; i.e., a file. Also,
readMapped will never
close a connection, even if it has to open it itself. For
readRaw
and
writeRaw if the connection is already open, it should have been opened in binary mode
by putting
"b" at the end of the
mode or
open
string.
E.g.,
file("input.dat",open="rb") for
readRead or
file("output.dat",open="wb") for
readRaw. Omitting
this
"b" on Windows will result in data corruption because newlines
in the file are converted to newline-return when read in non-binary mode.
what
a skeleton for the data to be read or mapped, which identifies the kind of data on
the connection and, if
length(what)>0 can specify how much to read.
n
if
>=0, read this many elements. If missing or
<0,
use
length(what) if this is positive, or just read all the data on the
connection (which better be a file).
object
the object whose data (only) will be written to the connection.
VALUE:
readMapped is the way to read binary data of elementary mode (e.g.,
numeric or integer) from a file. Typical applications would be to read data written
by another system or a C or Fortran program in binary form. This is also a way to
share data with other systems, provided you can figure out where these systems write
the data. As its name suggests
readMapped uses memory mapping to associate
the file with an S object, so the data is not physically copied and the function
is therefore suitable for large objects. The connection may be open in advance or
not: in either case
readMapped will never close it, since that would
invalidate the mapping. Notice that you can open a file and position it, with
seek,
in order to map to data not starting from the beginning of the file.
readRaw
is like
readMapped, but it does physically read the data, so
it is suitable (only) for connections that are not ordinary files and so cannot be
memory mapped.
writeRaw writes out the
length(object)
elementary data items in raw (binary) format, suitable for a non-S program to read.
SEE ALSO:
for reading data in text format.
EXAMPLES:
## Suppose bigPgm is a program that writes double precision binary data
## to its standard output.
shell("bigPgm > myData.out")
## now we can use myData.out repeatedly, by:
x = readMapped("myData.out", numeric())
## Alternately, to read the data once directly from bigPgm, but this
## time we need to know how much to read:
pgmPipe = pipe("bigPgm", open = "rb")
x = readRaw(pgmPipe, numeric(), 10000)
close(pgmPipe)