[Rd] Opening a file in mode "r+" or "r+b"

Laurens Leerink Laurens.Leerink at tudor.com
Fri May 23 14:48:01 MEST 2003


While using the file functions have found a few more issues (on both Unix
and Windows) ie

R documentation: 
In the "close" description (base package) we see that the possible values
for the mode 'open' the "r+" and "r+b" values are repeated, and are
incorrect the second time. The second set actually corresponds to the "w+" /
"w+b", see "man fopen" on unix.

File connections:
I had trouble using "r+" / "r+b" modes when processing files, all files
opened in this mode does not allow one to write to the file.  Saw that the
isOpen(con, "w") function always returns F in this mode, ie one can read
from them but cannot write.  After experimenting with flag combinations it
seems that all the other modes work well, so looked at the source code.  If
I'm not mistaken line 247 in connections.c should be changed from

    if(mlen >= 2 && con->mode[1] == '+') con->canread = TRUE;

to

    if(mlen >= 2 && con->mode[1] == '+') con->canwrite = TRUE;

This explained my earlier R script tests, ie that the only way to write to a
file is to truncate it ("w"/"w+" flag) or by appending to it ("a"/"ab").

Regards,
Laurens

>  -----Original Message-----
> From: 	Laurens Leerink  
> Sent:	Friday, May 23, 2003 9:33 AM
> To:	'r-help at stat.math.ethz.ch'
> Subject:	isSeekable returns F on seekable file
> 
> Hi,
> 
> Seems that on RWin 1.7.0 and 1.6.2 isSeekable returns F on binary files,
> while seek() works as expected on the same connection - see example below:
> 
> > con = file(nm, "rb")
> > isSeekable(con)
> [1] FALSE
> > readBin(con, double(), 10)
>  [1] 7.263824e-317 5.968155e-317 2.340685e-317 2.734062e-312 4.088386e-312
> 4.670335e-317
>  [7] 6.097545e-317 3.396341e-312 6.615484e-317 1.365171e-312
> > readBin(con, double(), 10)
>  [1] 1.303796e-317 5.577835e-317 3.409314e-312 1.303543e-317 3.893617e-317
> 4.077940e-312
>  [7] 6.910006e-313 2.694357e-318 4.088373e-312 6.484955e-317
> > seek(con, 0, origin="start")
> [1] 160
> > readBin(con, double(), 10)
>  [1] 7.263824e-317 5.968155e-317 2.340685e-317 2.734062e-312 4.088386e-312
> 4.670335e-317
>  [7] 6.097545e-317 3.396341e-312 6.615484e-317 1.365171e-312
> > 
> 
> Am I doing something silly or is the function returning the wrong value?
> 
> Regards,
> Laurens Leerink

	[[alternate HTML version deleted]]



More information about the R-devel mailing list