readLines(con = stdin(), n = -1L, ok = TRUE, warn = TRUE,
encoding = "unknown", skipNul = FALSE)
n > 0
lines are read? If not, an error will be generated.con
or
via options(encoding=)
: see the examples.
See also ‘Details’.
encoding
is
"latin1"
or "UTF-8"
,con
is a character string, the function calls
file
to obtain a file connection which is opened for
the duration of the function call. This can be a compressed file. If the connection is open it is read from its current position. If it
is not open, it is opened in "rt"
mode for the duration of
the call and then closed again. If the final line is incomplete (no final EOL marker) the behaviour
depends on whether the connection is blocking or not. For a
non-blocking text-mode connection the incomplete line is pushed back,
silently. For all other connections the line will be accepted, with a
warning. Whatever mode the connection is opened in, any of LF, CRLF or CR will
be accepted as the EOL marker for a line. Embedded nuls in the input stream will terminate the line currently
being read, with a warning (unless skipNul = TRUE
or warn
= FALSE)
). If con
is a not-already-open connection with a non-default
encoding
argument, the text is converted to UTF-8 and declared
as such (and the encoding
argument to readLines
is ignored).
See the examples.connections
, writeLines
, readBin
,
scan
cat("TITLE extra line", "2 3 5 7", "", "11 13 17", file = "ex.data",
sep = "\n")
readLines("ex.data", n = -1)
unlink("ex.data") # tidy up
## difference in blocking
cat("123\nabc", file = "test1")
readLines("test1") # line with a warning
con <- file("test1", "r", blocking = FALSE)
readLines(con) # empty
cat(" def\n", file = "test1", append = TRUE)
readLines(con) # gets both
close(con)
unlink("test1") # tidy up
## Not run: ------------------------------------
# # read a 'Windows Unicode' file
# A <- readLines(con <- file("Unicode.txt", encoding = "UCS-2LE"))
# close(con)
# unique(Encoding(A)) # will most likely be UTF-8
## ---------------------------------------------
Run the code above in your browser using DataLab