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, endian = .Platform$endian)
     writeBin(object, con, size = NA, endian = .Platform$endian)

_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"'.

       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 complex vectors.

  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.

_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.) Changing sizes is unlikely to preserve `NA's, and
     the extended precision sizes are unlikely to be portable across
     platforms.

_V_a_l_u_e:

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

     For `writeBin', 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'.

_S_e_e _A_l_s_o:

     `connection', `readLines', `writeLines'

_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)
     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)
     close(zz)
     unlink("testbin")
     stopifnot(z2 == z)

