Skip to content
Prev 31406 / 398506 Next

Scanning data files line-by-line

On Wed, 30 Apr 2003, R A F wrote:

            
You need to open the connection first.  readLines( "file", n = 1)
opens file "file", reads a line and then closes "file".  It has no idea
that you want to keep reading the same file.
It's open() you need, as in

con <- file("file")
open(con)
for(i in 1:10) print(readLines(con, n=1))
close(con)

In C you would need to (f)open a file to read it line-by-line, just as 
here.

The first two lines can be collapsed to

con <- file("file", "r")