I get unexpected behavior from "readLines()" and
"scan()" depending on how the file is opened with
"gzfile" or "unz". More specifically:
file <- gzfile("file.gz")
readLines(file,1)
It seems that the stream is rewound between calls to
readLines. The same is true if I replace readLines
with scan.
However, if I set argument 'open="r"', then
rewinding
does not occur:
file <- gzfile("file.gz",open="r")
readLines(file,1)
Once again, I get the same behavior for scan. The
rewinding behavior just described also appears if I
open a zip file with "unz".
file <- unz("file.zip", "file.txt")
readLines(file,1)
If I add the 'open="r"' argument to the call then I
get an error from readLines:
file <- unz("file.zip", "file.txt", open="r")
readLines(file,1)
Error in readLines(file, 1) : seek not enabled for
this connection
file <- unz("file.zip", "file.txt", open="rb")
readLines(file,1)
Error in readLines(file, 1) : seek not enabled for
this connection
However, if I instead use "scan" to read the file,
then there are no errors and I get the rewind/no
rewind behavior described above.
file <- unz("file.zip", "file.txt")
scan(file,nlines=1,sep="\t",what=character(0))
Read 3 items
[1] "a" "b" "c"
scan(file,nlines=1,sep="\t",what=character(0))
Read 3 items
[1] "a" "b" "c"
file <- unz("file.zip", "file.txt", open="r")
scan(file,nlines=1,sep="\t",what=character(0))
Read 3 items
[1] "a" "b" "c"
scan(file,nlines=1,sep="\t",what=character(0))
Read 3 items
[1] "1" "2" "3"