[Rd] scan after seek in text files (PR#12640)
alex at apeironsports.com
alex at apeironsports.com
Sat Aug 30 00:40:07 CEST 2008
Full_Name: Dr. Alex Sheppard
Version: 2.7.1
OS: Linux Debian Lenny
Submission from: (NULL) (79.73.224.62)
After scanning from an open (text) connection, then seeking, then scanning
again, the second scan returns incorrect result. It looks like the first byte
scanned was from the pre-seek file position, then it continues to read from the
post-seek file position.
To reproduce:
#Put 3x3 matrix in a file
> write.matrix(t(matrix(1:9,nrow=3)),file="TEST.txt",sep="\t")
#Open file as text
> fd <- file("TEST.txt",open="rt")
#scan a couple of fields - this looks fine so far
> scan(file=fd,what=double(),n=2)
Read 2 items
[1] 1 2
#seek back to start of file
> seek(con=fd,where=0,origin="start")
[1] 5
#scan fields again - this doesn't work properly
> scan(file=fd,what=double(),n=2)
Read 2 items
[1] 31 2
This happens when either n or nmax arguments are used to control number of
fields read. Problem does not occur when using nlines argument instead. The seek
appears to work ok, as doing readChar(fd,n=1) after the seek operation correctly
returns "1".
Also, if the file is opened as binary, i.e. fd=file("TEST.txt",open="rb") , all
works fine.
More information about the R-devel
mailing list