[Rd] writeChar

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Aug 29 08:38:48 CEST 2006


Given that if you specify the 'nchars' argument you are trying to do 
something unnatural, I don't see why you find it incorrect to get warnings 
about mismatches between what you supply and what you asked for, but 
expect one if what you supply matches what you asked for (and truncation 
occurs).  Originally it was an error to ask for more characters than were 
available, but Fritz Leisch persuaded us to allow this with a warning
(and that is precisely what the documentation says).  Since then we have 
added the raw type, and I would now say that if you want to do things 
involving nul bytes, you should be using the raw type instead.

R help normally tells you its recycling rules if there are any, and surely 
it is no hardship to supply an accurate version of 'nchars' using rep()?
Especially as almost all users are not going to be specifying 'nchars'?

I've added a stricter sanity check against incorrect specification of 
'nchars'.

On Mon, 28 Aug 2006, Kasper Daniel Hansen wrote:

> 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
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-devel mailing list