Skip to content
Prev 293484 / 398503 Next

Help with readBin

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