readBin                 package:base                 R Documentation

_T_r_a_n_s_f_e_r _B_i_n_a_r_y _D_a_t_a _T_o _a_n_d _F_r_o_m _C_o_n_n_e_c_t_i_o_n_s

_D_e_s_c_r_i_p_t_i_o_n:

     Read binary data from a connection, or write binary data to a
     connection.

_U_s_a_g_e:

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

     readChar(con, nchars)
     writeChar(object, con, nchars = nchar(object), eos = "")

_A_r_g_u_m_e_n_t_s:

     con: A connection object or a character string.

    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', 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.

  object: An R object to be written to the connection.

  nchars: integer, giving the lengths of (unterminated) character
          strings to be read or written.

     eos: character. The terminator to be written after each string,
          followed by an ASCII 'nul'; use 'NULL' for no terminator at
          all.

_D_e_t_a_i_l_s:

     If the 'con' is a character string, the functions call 'file' to
     obtain an 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 and then closed again.

     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 'NA's, 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' allow more flexibility, and can also be
     used on text-mode connections.

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

_V_a_l_u_e:

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

     For 'readChar', a character vector of length the number of items
     read (which might be less than 'length(nchars)').

     For 'writeBin' and 'writeChar', none.

_N_o_t_e:

     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'.

     As from R 2.0.0, character strings containing ASCII 'nul' will be
     read correctly by 'readChar' and appear as embedded nuls in the
     character vector returned.  (In earlier versions of R they
     terminated the strings returned.)

     If the character length requested for 'readChar' is longer than
     the data available on the connection, what is available is
     returned. For 'writeChar' if too many characters are requested the
     output is zero-padded, with a warning.

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

_S_e_e _A_l_s_o:

     The _R Data Import/Export_ manual.

     'connections', 'readLines', 'writeLines'.

     '.Machine' for the sizes of 'long', 'long long' and 'long double'.

_E_x_a_m_p_l_e_s:

     zz <- file("testbin", "wb")
     writeBin(1:10, zz)
     writeBin(pi, zz, endian="swap")
     writeBin(pi, zz, size=4)
     writeBin(pi^2, zz, size=4, endian="swap")
     writeBin(pi+3i, zz)
     writeBin("A test of a connection", zz)
     z <- paste("A very long string", 1:100, collapse=" + ")
     writeBin(z, zz)
     if(.Machine$sizeof.long == 8 || .Machine$sizeof.longlong == 8)
         writeBin(as.integer(5^(1:10)), zz, size = 8)
     if((s <-.Machine$sizeof.longdouble) > 8)
         writeBin((pi/3)^(1:10), zz, size = s)
     close(zz)

     zz <- file("testbin", "rb")
     readBin(zz, integer(), 4)
     readBin(zz, integer(), 6)
     readBin(zz, numeric(), 1, endian="swap")
     readBin(zz, numeric(), size=4)
     readBin(zz, numeric(), size=4, endian="swap")
     readBin(zz, complex(), 1)
     readBin(zz, character(), 1)
     z2 <- readBin(zz, character(), 1)
     if(.Machine$sizeof.long == 8 || .Machine$sizeof.longlong == 8)
         readBin(zz, integer(), 10,  size = 8)
     if((s <-.Machine$sizeof.longdouble) > 8)
         readBin(zz, numeric(), 10, size = s)
     close(zz)
     unlink("testbin")
     stopifnot(z2 == z)

     ## test fixed-length strings
     zz <- file("testbin", "wb")
     x <- c("a", "this will be truncated", "abc")
     nc <- c(3, 10, 3)
     writeChar(x, zz, nc, eos=NULL)
     writeChar(x, zz, eos="\r\n")
     close(zz)

     zz <- file("testbin", "rb")
     readChar(zz, nc)
     readChar(zz, nchar(x)+3) # need to read the terminator explicitly
     close(zz)
     unlink("testbin")

     ## signed vs unsigned ints
     zz <- file("testbin", "wb")
     x <- as.integer(seq(0, 255, 32))
     writeBin(x, zz, size=1)
     writeBin(x, zz, size=1)
     x <- as.integer(seq(0, 60000, 10000))
     writeBin(x, zz, size=2)
     writeBin(x, zz, size=2)
     close(zz)
     zz <- file("testbin", "rb")
     readBin(zz, integer(), 8, size=1)
     readBin(zz, integer(), 8, size=1, signed=FALSE)
     readBin(zz, integer(), 7, size=2)
     readBin(zz, integer(), 7, size=2, signed=FALSE)
     close(zz)
     unlink("testbin")

