Read and write known-length character data from and to a connection

DESCRIPTION:

Read and write known-length character data from and to a connection. The strings do not have to be null terminated but their lengths must be given.

USAGE:

readChar(con, nchars)
writeChar(object, con, nchars = nchar(object), eos = "")

ARGUMENTS:

object
character vector containing data to write
con
a connection object or a character string (taken to be 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 open, it will be opened in binary mode and after the data is processed it will be closed.
nchars
The lengths of the character strings. For readChar this is the number of bytes in the file to read for each string and for writeChar this is the number of bytes to write (not including the eos string). length(nchars) is the number of strings to read or write. writeChar will pad a string with null bytes if it is shorter than its corresponding nchars value.
eos
A scalar 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.

VALUE:

readChar returns the strings read and writeChar returns NULL.

SEE ALSO:

and read and write null-terminated strings

EXAMPLES:

  tf <- tempfile()
  cat(file=tf, "AbcDefgHi")
  readChar(tf, nchars=c(3,3,2)) # get "Abc", "Def", "Hi"
  unlink(tf)