[Rd] seek(con, 0, "end",
rw="r") does not always work correctly (PR#7896)
tplate at blackmesacapital.com
tplate at blackmesacapital.com
Wed May 25 22:37:12 CEST 2005
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?]
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
More information about the R-devel
mailing list