[Rd] scan after seek in text files (PR#12640)

From: <alex_at_apeironsports.com>
Date: Sat, 30 Aug 2008 00:40:07 +0200 (CEST)

Full_Name: Dr. Alex Sheppard
Version: 2.7.1
OS: Linux Debian Lenny
Submission from: (NULL) (

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.

R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Sat 30 Aug 2008 - 07:00:45 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 01 Sep 2008 - 10:38:00 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive