Examples for 'base::readLines'


Read Text Lines from a Connection

Aliases: readLines

Keywords: file connection

### ** Examples

fil <- tempfile(fileext = ".data")
cat("TITLE extra line", "2 3 5 7", "", "11 13 17", file = fil,
    sep = "\n")
readLines(fil, n = -1)
[1] "TITLE extra line" "2 3 5 7"          ""                 "11 13 17"        
unlink(fil) # tidy up

## difference in blocking
fil <- tempfile("test")
cat("123\nabc", file = fil)
readLines(fil) # line with a warning
Warning in readLines(fil): incomplete final line found on
'/tmp/Rtmpul02DQ/testa43702e16e67c'
[1] "123" "abc"
con <- file(fil, "r", blocking = FALSE)
readLines(con) # empty
[1] "123"
cat(" def\n", file = fil, append = TRUE)
readLines(con) # gets both
[1] "abc def"
close(con)

unlink(fil) # tidy up

## Not run: 
##D # read a 'Windows Unicode' file
##D A <- readLines(con <- file("Unicode.txt", encoding = "UCS-2LE"))
##D close(con)
##D unique(Encoding(A)) # will most likely be UTF-8
## End(Not run)

[Package base version 4.2.3 Index]