[Rd] seek(con, 0, "end", rw="r") does not always work correctly (PR#7899)

ligges at statistik.uni-dortmund.de ligges at statistik.uni-dortmund.de
Thu May 26 17:31:02 CEST 2005


tplate at blackmesacapital.com wrote:

> I've noticed that seek(con, 0, "end", rw="r") on a file connection does 
> not always work correctly after a write (R 2.1.0 on Windows).
> 
> [Is a call to fflush() needed inside file_seek() in main/connections.c?]


If you have an idea where to fflush() precisely and your patch works, 
please tell it! I'll happily run some test cases where seeking matters.

Note that ?seek currently tells us "The value returned by seek(where=NA) 
appears to be unreliable on Windows systems, at least for text files."
It would be nice if this comment could be removed, of course ....

Uwe Ligges



> Example (see the lines with the "***WRONG***" comment)
> 
>  > # seek(, rw="r") on a file does not always work correctly after a write
>  > f <- file("tmp3.txt", "w+b")
>  > # Write something earlier in the file
>  > seek(f, 10, rw="w")
> [1] 0
>  > writeLines(c("ghi", "jkl"), f)
>  > seek(f, 20, rw="w")
> [1] 18
>  > writeLines(c("abc"), f)
>  > seek(f, 0, "end", rw="w")
> [1] 24
>  > # Try to read at the end of the file
>  > seek(f, 0, "end", rw="r")
> [1] 0
>  > readLines(f, -1)
> character(0)
>  > seek(f, 0, "end", rw="w")
> [1] 18
>  > # write something at the end of the file
>  > writeLines(c("def"), f)
>  > # Try to read at the end of the file
>  > # flush(f) # flushing here makes the seek work correctly
>  > seek(f, 0, "end", rw="r")
> [1] 24
>  > seek(f, NA, rw="r") # ***WRONG*** (should return 28)
> [1] 24
>  > readLines(f, -1) # ***WRONG*** (should return character(0))
> [1] "def"
>  > seek(f, 20, rw="r")
> [1] 28
>  > readLines(f, -1)
> [1] "abc" "def"
>  > seek(f, 0, "end", rw="r") # now it works correctly
> [1] 28
>  > seek(f, NA, rw="r")
> [1] 28
>  > readLines(f, -1)
> character(0)
>  > close(f)
>  >
>  > version
>           _
> platform i386-pc-mingw32
> arch     i386
> os       mingw32
> system   i386, mingw32
> status
> major    2
> minor    1.0
> year     2005
> month    04
> day      18
> language R
>  >
> 
> -- Tony Plate
> 
> ______________________________________________
> R-devel at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list