[Rd] writeChar
Kasper Daniel Hansen
khansen at stat.Berkeley.EDU
Tue Aug 29 03:40:23 CEST 2006
I have recently been using writeChar and writeBin to write binary
files. These functions makes it very easy to write OS-independent
files, which I am very happy with.
I have however found a few issues, best illustrated by a short example
> con <- file("test", open = "wb")
> writeChar(as.character(c("ab", "ab")), nchars = c(3), con = con,
eos = "")
Warning message:
writeChar: more characters requested than are in the string - will
zero-pad
> close(con)
> system("hexdump -Cv test")
00000000 61 62 00 00 |ab..|
00000004
As seen from this example, the fact that nchars is shorter than the
character vector being written results in the remaining elements
being skipped. Also, the nchars argument is not being recycled,
something I find quite R-counter-intuitive. I would suggest
recycling, although I am not sure how to best achieve this.
I also get a warning about zero-padding - even if I request more
characters than present in the vector. This means that I get a
warning if using the argument for "what is what meant for" (at least
in my opinion). Right now I get many warnings when writing the binary
files I am working with, which might be confusing to future users. I
would suggest to only give a warning if requesting _less_ characters,
since in that case R would truncate the argument.
Finally, having a NA or a negative number (not shown) in the nchars
argument makes R crash, at least on two platforms:
> con <- file("test", open = "wb")
> writeChar(as.character(c("ab", "ab")), nchars = c(3, NA), con =
con, eos = NULL)
*** caught bus error ***
address 0x2008000, cause 'invalid alignment'
This is reproducible using R-2.3.0 (patched) on 32bit Linux:
> con <- file("test", open = "wb")
> writeChar(as.character(c("ab", "ab")), nchars = c(3, NA), con =
con, eos = NULL)
*** caught segfault ***
address 0x1a5c000, cause 'memory not mapped'
Segmentation fault
I have not tested it on R-devel, but the news file does not indicate
any changes in writeChar.
Kasper
More information about the R-devel
mailing list