readChar(con, nchars, useBytes = FALSE) writeChar(object, con, nchars = nchar(object, type = "chars"), eos = "", useBytes = FALSE)
object | a character vector containing the data to write. |
con | a connection object or a character string (read as a file name). If the connection is open, it should be open in binary mode and open for reading or writing, as appropriate. If it is not yet open, it is opened in binary mode; after the data is processed, it is closed. |
nchars | the lengths of the character strings. For readChar, this is the number of bytes in the file to read for each string. For writeChar, this is the number of bytes to write (not including the eos string). writeChar pads a string with null bytes if it is shorter than its corresponding nchars value. |
eos | A string to be written after each string in object. The trailing null byte is also written. Use eos=NULL to mean not to write anything after each string. |
useBytes |
For readChar, if useBytes is TRUE,
the bytes in the text strings are read as sequences of
single-byte characters in the latin1 (ISO-8859-1) encoding.
If FALSE (the default), these strings are
interpreted as sequences of Unicode characters in the UTF-8 encoding,
some of which may be encoded as multiple bytes.
For writeChar, useBytes controls the writing of both object and eos. If useBytes is TRUE, writeChar writes the bytes in the text strings as produced by charToRaw. If useBytes is FALSE (the default), the text strings are written as as sequences of Unicode characters in the UTF-8 encoding. In addition, if any element of object or eos is a string with encoding "bytes", this string is written as if useBytes was TRUE. |
readChar | returns the strings read. |
writeChar | nothing |
con <- tempfile() writeChar("123", con, 3) readChar(con, 3) # returns "123"writeChar( c("abc", "def"), con, c(3, 3)) readChar(con, c(4, 4)) # returns "abc" "def" # must specify nchars=c(4,4) to read terminating 0 bytes
writeChar("123", con, 5) # produces warning message: # more characters requested than are in the string - will zero-pad readChar(con, 3) # returns "123"
writeChar(c("abc","def"), con, c(3,3), eos=NULL) # write strings without ending 0 bytes readChar(con, 6) # returns "abcdef"