Rev 44421 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/readChar.Rd% Part of the R package, http://www.R-project.org% Copyright 1995-2007 R Core Development Team% Distributed under GPL 2 or later\name{readChar}\alias{readChar}\alias{writeChar}\title{Transfer Character Strings To and From Connections}\description{Transfer character strings to and from connections, without assumingthey are null-terminated on the connection.}\usage{readChar(con, nchars, useBytes = FALSE)writeChar(object, con,nchars = nchar(object, type="chars"), eos = "")}\arguments{\item{con}{A connection object, or a character string naming a file,or a raw vector.}\item{nchars}{integer, giving the lengths in characters of (unterminated)character strings to be read or written. Must be >= 0 and notmissing.}\item{useBytes}{logical: should \code{nchars} be regarded as a numberof bytes not characters in a multi-byte locale?}\item{object}{A character vector to be written to the connection, atleast as long as \code{nchars}.}\item{eos}{\sQuote{end of string}: character string . The terminatorto be written after each string, followed by an ASCII \code{nul};use \code{NULL} for no terminator at all.}}\details{These functions complement \code{\link{readBin}} and\code{\link{writeBin}} which read and write C-style zero-terminatedcharacter strings. They are for strings of known length, andcan optionally write an end-of-string mark. They are intended onlyfor character strings valid in the current locale.If \code{con} is a character string, the functions call\code{\link{file}} to obtain an file connection which is opened forthe duration of the function call.If the connection is open it is read/written from its currentposition. If it is not open, it is opened for the duration of the calland then closed again. Connections can be open in either text orbinary mode.If \code{readChar} is called with \code{con} a raw vector, the data inthe vector is used as input. If \code{writeChar} is called with\code{con} a raw vector, it is just an indication that a raw vectorshould be returned.Character strings containing ASCII \code{nul}(s) will be readcorrectly by \code{readChar} and appear with embedded nuls in thecharacter vector returned. \code{writeChar} can write strings withembedded \code{nul}s, and for such strings inteprets \code{nchar} asthe number of bytes to be written.If the character length requested for \code{readChar} is longer thanthe data available on the connection, what is available isreturned. For \code{writeChar} if too many characters are requestedthe output is zero-padded, with a warning.Missing strings are written as \code{NA}.}\value{For \code{readChar}, a character vector of length the number ofitems read (which might be less than \code{length(nchars)}).For \code{writeChar}, a raw vector (if \code{con} is a raw vector) orinvisibly \code{NULL}.}\note{\code{nchars} will be interpreted in bytes not characters in anon-UTF-8 multi-byte locale, with a warning.There is little validity checking of UTF-8 reads.}\seealso{The \emph{R Data Import/Export} manual.\code{\link{connections}}, \code{\link{readLines}},\code{\link{writeLines}}, \code{\link{readBin}}}\examples{## test fixed-length stringszz <- 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 explicitlyclose(zz)unlink("testchar")}\keyword{file}\keyword{connection}