[R] Using readBin to read binary "unformatted" output files from Fortran?

Duncan Murdoch murdoch.duncan at gmail.com
Wed Feb 15 11:41:55 CET 2012


On 12-02-14 10:58 PM, Jooil Kim wrote:
> Hello,
>
> I'm wondering if I can get some help with reading Fortran binary "unformatted" output files into R.
>
> The Fortran output files were generated in Ubuntu 10.04 LTS using gfortran4.4, on a 32bit Intel Core 2 Duo 3.16 GHz machine, with little-endian and record marker lengths equal to 4.
>
> The machine I'm currently trying to read this Fortran output file is a Macbook Pro running Lion 10.7.3, on a 2.26 GHz Intel Core 2 Duo with 4Gb of ram. I'm running R 2.14.1.
>
> Based on whatever information I could gather from the web, I've been trying out the following code (the name of the Fortran output file is "header", located in the current working directory).
>
>> to.read<- file("header", "rb")
>> readBin(to.read, "integer", n=2, size = 4, endian = "little")
>
> Unfortunately, these commands return empty.
>
> Am I on the right track here? Is what I'm trying to accomplish here even technically possible?

Yes, that should have worked.  To debug, I'd replace your second line with

readBin(to.read, "raw", n=100)

For example, when I create a file by writing out 1:10 using writeBin, 
that gives

 > readBin(to.read, "raw", n=100)
  [1] 01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 05 00 00
[20] 00 06 00 00 00 07 00 00 00 08 00 00 00 09 00 00 00 0a 00
[39] 00 00

from which it is pretty obvious the file contains small integers in 
little endian form.  Paul Murrell's hexView package gives more elaborate 
possibilities.

Duncan Murdoch



More information about the R-help mailing list