readChar                package:base                R Documentation

_T_r_a_n_s_f_e_r _C_h_a_r_a_c_t_e_r _S_t_r_i_n_g_s _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:

     Transfer character strings to and from connections, without
     assuming they are null-terminated on the connection.

_U_s_a_g_e:

     readChar(con, nchars)

     writeChar(object, con,
               nchars = nchar(object, type="chars"), eos = "")

_A_r_g_u_m_e_n_t_s:

     con: A connection object or a character string naming a file.

  nchars: integer, giving the lengths in characters of (unterminated)
          character strings to be read or written. Must be >= 0 and not
          missing.

  object: A character vector to be written to the connection, at least
          as long as 'nchars'.

     eos: 'end of string': character string .  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:

     These functions complement 'readBin' and 'writeBin' which read and
     write C-style zero-terminated character strings.  They are for
     strings of known length, and can optionally write an end-of-string
     mark.  They are intended only for character strings valid in the
     current locale.

     If '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.  Connections can be open in either
     text or binary mode.

     In a single-byte locale, character strings containing ASCII
     'nul'(s) will be read correctly by 'readChar' and appear with
     embedded nuls in the character vector returned.  This may not work
     for multi-byte locales, and does not work for 'writeChar'.

     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.

     Missing strings are written as 'NA'.

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

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

     For 'writeChar' none (strictly, invisible 'NULL').

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

     The _R Data Import/Export_ manual.

     'connections', 'readLines', 'writeLines', 'readBin'

_E_x_a_m_p_l_e_s:

     ## test fixed-length strings
     zz <- file("testchar", "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("testchar", "rb")
     readChar(zz, nc)
     readChar(zz, nchar(x)+3) # need to read the terminator explicitly
     close(zz)
     unlink("testchar")

