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, useBytes = FALSE) 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, or a raw vector. nchars: integer, giving the lengths in characters of (unterminated) character strings to be read or written. Must be >= 0 and not missing. useBytes: logical: should 'nchars' be regarded as a number of bytes not characters in a multi-byte locale? 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 in an appropriate mode (binary read or write) and then closed again. An open connection must be in binary mode. If 'readChar' is called with 'con' a raw vector, the data in the vector is used as input. If 'writeChar' is called with 'con' a raw vector, it is just an indication that a raw vector should be returned. Character strings containing ASCII 'nul'(s) will be read correctly by 'readChar' but truncated at the first 'nul' with a warning. 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', a raw vector (if 'con' is a raw vector) or invisibly 'NULL'. _N_o_t_e: Earlier versions of R allowed embedded nul bytes within character strings, but not R >= 2.8.0. 'readChar' was commonly used to read fixed-size zero-padded byte fields for which 'readBin' was unsuitable. 'readChar' can still be used for such fields if there are no embedded nuls: otherwise 'readBin(what="raw")' provides an alternative. 'nchars' will be interpreted in bytes not characters in a non-UTF-8 multi-byte locale, with a warning. There is little validity checking of UTF-8 reads. _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")