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

David Winsemius dwinsemius at comcast.net
Wed Feb 15 22:47:50 CET 2012


On Feb 15, 2012, at 5:41 AM, Duncan Murdoch wrote:

> 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).

Jooil Kim;

Are you sure that is the complete file name? The MacOS (like Windows)  
will hide the extensions of some files when using Finder.app unless  
you change the defaults.

-- 
David.
>>
>>> 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
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list