[Rd] Strange behaviour of read and writeBin

Duncan Murdoch murdoch.duncan at gmail.com
Fri Feb 4 14:52:32 CET 2011


On 04/02/2011 5:35 AM, Christian Ruckert wrote:
> To me it seems like writeBin() writes one char/byte more than expected.

You want writeChar rather than writeBin to avoid the null termination of 
strings.

Duncan Murdoch

>   >  con<- file("testbin", "wb")
>   >  writeBin("ttccggaa", con)
>   >  close(con)
>
>   >  con<- file("testbin", "rb")
>   >  readBin(con, what="character")
> [1] "ttccggaa"
>   >  seek(con, what=NA)
> [1] 9
>   >  close(con)
>
>   >  con<- file("testbin", "rb")
>   >  readBin(con, what="raw", n=20)
> [1] 74 74 63 63 67 67 61 61 00
>   >  seek(con, what=NA)
> [1] 9
>   >  close(con)
>
> As the numbering starts with 0 the position should be 8 and not 9 after
> reading. There were two older threads which look very similar to my problem:
>
> http://tolstoy.newcastle.edu.au/R/e2/devel/06/11/1119.html
> http://r.789695.n4.nabble.com/Re-Problem-reading-binaries-created-with-fortran-More-infos-td974396.html
>
> Thanks in advance,
> Christian
>
>
>
>   >  sessionInfo()
> R version 2.12.0 (2010-10-15)
> Platform: x86_64-pc-linux-gnu (64-bit)
>
> locale:
> [1] C
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> other attached packages:
> [1] Biostrings_2.18.2 IRanges_1.8.8
>
> loaded via a namespace (and not attached):
> [1] Biobase_2.10.0
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list