[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  readChar: Transfer Character Strings To and From Connections

##  Description

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

##  Usage

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

##  Arguments

con

A [ connection ](/link/connection?package=base&version=3.6.2) object, or a
character string naming a file, or a raw vector.

nchars

integer vector, giving the lengths in characters of (unterminated) character
strings to be read or written. Elements must be >= 0 and not ` NA ` .

useBytes

logical: For ` readChar ` , should ` nchars ` be regarded as a number of bytes
not characters in a multi-byte locale? For ` writeChar ` , see ` [ writeLines
](/link/writeLines?package=base&version=3.6.2) ` .

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.

##  Value

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 ` .

##  Details

These functions complement ` [ readBin
](/link/readBin?package=base&version=3.6.2) ` and ` [ writeBin
](/link/writeBin?package=base&version=3.6.2) ` 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.

These functions are intended to be used with binary-mode connections. If ` con
` is a character string, the functions call ` [ file
](/link/file?package=base&version=3.6.2) ` to obtain a binary-mode 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 ` .

##  See Also

The ‘R Data Import/Export’ manual.

` [ connections ](/link/connections?package=base&version=3.6.2) ` , ` [
readLines ](/link/readLines?package=base&version=3.6.2) ` , ` [ writeLines
](/link/writeLines?package=base&version=3.6.2) ` , ` [ readBin
](/link/readBin?package=base&version=3.6.2) `

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2FreadChar&utm_source=r-docs&utm_medium=docs&utm_term=readChar&utm_content=run_example_in_workspace)

    
    
    # NOT RUN {
    ## test fixed-length strings
    zzfil <- tempfile("testchar")
    zz <- file(zzfil, "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(zzfil, "rb")
    readChar(zz, n