[R] Reading single precision floats from binary file

Peter Dalgaard BSA
2001-02-15T17:12:27Z

Kjetil Kjernsmo <kjetil.kjernsmo at astro.uio.no> writes:

> Dear all,
>
> I have a few files with binary data written by a C program a friend wrote.
> I allready have program to read these files, Thomas Lumley and Prof Brian
> D Ripley was kind enough to respond to a question to this list earlier
> with some code that works perfectly, so this is really no problem, it is
> more out curiousity.
>
> Most of my files consists of 40000 single precision floats, and I figured
> the new readBin() function exists for the purpose of reading such files,
> so I tried to use it, but with no success.
>
> Now, the docs say that what you can read is "numeric",
"double",
> "integer", "int", "logical",
"complex", "character", and since "single" is
> not on the list, is it simply not the right tool for the job?
> I tried different things, among them, providing single as what,
> changing the size, etc, but it always ends up with reading 20000 records,
> indicating that it does read the file as double precision floats.
>
> It is not clear to me what the last sentence in the notes for this
> function means, and indeed not quite clear what the function does at all,
> but I was hoping someone can enlighten me.


Works OK for me with the current patch version, except that the nparameter to
readBin seems to be misdocumented:

x<-rnorm(100)
zz <- file("testbin", "wb")
writeBin(x, zz, size=4)
close(zz)
system("ls -l testbin")

zz <- file("testbin", "rb")
xx <- readBin(zz, numeric(), n= 999999, size=4)
length(xx)
range(x - xx)
close(zz)

--
   O__  ---- Peter Dalgaard             Blegdamsvej 3
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907