[R] Help with readBin

William Dunlap wdunlap at tibco.com
Thu May 3 23:00:19 CEST 2012


You can do the following to allow others to recreate your problem.

  yourFileBytes <- readBin("yourFile", what="integer", size=1, n=300) # is 300 bytes enough to see the problem?
  dput(yourFileBytes)

Put the output of dput(yourFileBytes) in your mail.  Someone can (and you should)
recreate the problem with
  bytes <- ... copy 'n paste the printout of dput(bytes) here ...
  tf <- tempfile()
  stopifnot(is.integer(bytes) && all(abs(bytes)<=128)) # to make sure bytes was copied correctly
  writeBin(bytes, con=tf, size=1)

Then show just the commands needed to read a couple of rows of your file, along with
the expected output, as precisely and you can.  E.g.,
  con <- file(tf, "rb")
  readBin(con, what="integer", size=4, n=2) # expect 3 then something less than 10
  readBin(con, what="numeric", size=8, n=3) # expect 2 numbers in range (0, 32] then 2.57
  ...

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of kapo coulibaly
> Sent: Thursday, May 03, 2012 10:57 AM
> To: r-help at r-project.org
> Subject: Re: [R] Help with readBin
> 
>  I believe here is the structure of the file I'm trying to read:
> record marker (4 bytes), 2 integers (4 bytes each), 2 doubles (8 bytes
> each), one string (16 bytes or 16 characters), 3 integers (4 bytes each), 1
> record marker (4 bytes) and a big array of doubles (8 bytes each).
> Everything in the file is read correctly except for the doubles.
> If any indication, I've read similar file before with readBin the only
> difference is this one was created with a code compiled with gfortran in
> linux 64 bit. I was able to read the same output binary file when the
> fortran source code was compiled in windows xp 32 bit. The values I'm
> expecting should be between 0 and about 32.
> 
> 
> 
> 
> The code I used is:
> 
> 
> 
> # Loading Required libraries
> library(tcltk)
> 
> # Tk inputbox function
>  inputBox<-function() {
>   tt<-tktoplevel()
>   Zmin<-tclVar("0")
>   Zmax<-tclVar("0")
>   dZ<-tclVar("0")
>   entry.Zmin<-tkentry(tt,width="20",textvariable=Zmin)
>   entry.Zmax<-tkentry(tt,width="20",textvariable=Zmax)
>   entry.dZ<-tkentry(tt,width="20",textvariable=dZ)
>   lbl.Zmin<-tklabel(tt,text="Number of layers")
>   lbl.Zmax<-tklabel(tt,text="Number of Stress Periods")
>   lbl.dZ<-tklabel(tt,text="dZ")
>   tkgrid(lbl.Zmin,entry.Zmin)
>   tkgrid(entry.Zmin)
>   tkgrid(lbl.Zmax,entry.Zmax)
>   tkgrid(entry.Zmax)
>   #tkgrid(lbl.dZ,entry.dZ)
>   #tkgrid(entry.dZ)
> 
>   OnOK <- function()
>   {
>       # NameVal <- c(tclvalue(Zmin),tclvalue(Zmax),tclvalue(dZ))
>         tkdestroy(tt)
>     }
>   OK.but <-tkbutton(tt,text="   OK   ",command=OnOK)
>   # tkbind(entry.Name, "<Return>",OnOK)
>   tkgrid(OK.but,columnspan=3)
>   tkfocus(tt)
>   tkwait.window(tt)
>   res<-as.numeric(c(tclvalue(Zmin),tclvalue(Zmax)))#,tclvalue(dZ)))
>   return(res)
> }
> 
> ########################################################################
> ########
> # Main program
> ########################################################################
> ########
> 
> # Model Parameters input (number of layers and stress periods)
> param<-inputBox()
> 
> # Select and open Modflow Binary file for reading
> fich<-tclvalue(tkgetOpenFile(title="Modflow Binary File",filetypes="{{hds
> binary Files} {.hds}} {{All files} *}"))
> zz <- file(fich, "rb")
> 
> # Cycling thru time steps and layers
> for (k in 1:param[2]) {
>   for (i in 1:param[1]) {
>     readBin(zz,what="numeric",n=1,size=4) # record marker typical of
> fortran access="sequential" in gfortran
>     readBin(zz,what="integer",n=2,size=4)->N1
>     readBin(zz,what="double",n=2,size=8)->N2
>     readChar(zz,16)->txt1
>     print(txt1)
>     readBin(zz,what="integer",n=3,size=4)->N3
>     tnber<-N3[1]*N3[2]
>     readBin(zz,what="integer",n=1,size=4) # record marker typical of
> fortran access="sequential" in gfortran
>     readBin(zz,what=real(),n=tnber,size=4)->N4
>     readBin(zz,what="integer",n=2,size=4) # record marker typical of
> fortran access="sequential" in gfortran
>     print(N4[1:10])
> 
> 
>     }
> 
> }
> 
> close(zz)
> 
> On Thu, May 3, 2012 at 1:26 PM, Duncan Murdoch
> <murdoch.duncan at gmail.com>wrote:
> 
> > On 03/05/2012 12:41 PM, kapo coulibaly wrote:
> >
> >> I'm trying to read a binary file created by a fortran code using readBin
> >> and readChar. Everything reads fine (integers and strings) except for
> >> double precision numbers, they are read as huge or very small number
> >> (1E-250,...). I tried various endianness, swap, But nothing has worked so
> >> far.
> >> I also tried on R 64 bit for linux and windows (R 2.14) and R 2.11 on
> >> windows XP 32 bit.
> >> Any help would be appreciated.
> >>
> >
> > As I wrote to someone else with a similar problem a couple of weeks ago:
> >
> > You need to see what's in the file.  The hexView package can dump it in
> > various formats; see example(viewFormat) for a couple.
> >
> > Duncan Murdoch
> >
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list