[R-sig-Geo] reading select values from a binary file

Jonathan Thayn jthayn at ilstu.edu
Wed Dec 16 19:12:48 CET 2009


I tried using the seek() function, but it did not work as I thought it would. Perhaps I have done something wrong:

> writeBin(1:20,"desktop/test.bip",size=2,endian="big")
> cc <- file("desktop/test.bip","rb")
> readBin(cc,"integer",n=1,size=2,endian="big")
[1] 1
> seek(cc,13)
[1] 2 
> readBin(cc,"integer",n=1,size=2,endian="big")
[1] 1792 #this is not correct
> seek(cc,0)
[1] 15
> readBin(cc,"integer",n=1,size=2,endian="big")
[1] 1 #the seek(cc,0) seems to have reset it
> flush(cc)
> seek(cc,6)
[1] 2
> readBin(cc,"integer",n=1,size=2,endian="big")
[1] 4 #this is not the sixth number
> readBin(cc,"integer",n=1,size=2,endian="big")
[1] 5
> flush(cc)
> seek(cc,12)
[1] 10
> readBin(cc,"integer",n=1,size=2,endian="big")
[1] 7 #this is not the twelfth number
> flush(cc)
> seek(cc,1)
[1] 14
> readBin(cc,"integer",n=1,size=2,endian="big")
[1] 256
> seek(cc,0)
[1] 3
> readBin(cc,"integer",n=1,size=2,endian="big")
[1] 1
> seek(cc,18)
[1] 2
> readBin(cc,"integer",n=1,size=2,endian="big")
[1] 10


I tried the readBinFragments() function in the R.utils package and it works, so long as I use a seek(cc,0) between calls:

readBinFragments(cc,"integer",idxs=c(2,6,7,13,19),size=2,endian="big")
[1]  2  6  7 13 19
> readBinFragments(cc,"integer",idxs=c(4,9,12),size=2,endian="big")
[1] 0 0 0
> flush(cc)
> readBinFragments(cc,"integer",idxs=c(4,9,12),size=2,endian="big")
[1] 0 0 0
> seek(cc,0)
[1] 52
> readBinFragments(cc,"integer",idxs=c(4,9,12),size=2,endian="big")
[1]  4  9 12


Am I using seek() incorrectly?



Jonathan B. Thayn




On Dec 9, 2009, at 12:33 PM, Barry Rowlingson wrote:

> On Wed, Dec 9, 2009 at 6:20 PM, rick reeves <reeves at nceas.ucsb.edu> wrote:
>> Hello All:
>> Faced with a similar challenge, and NOT wanting to resort to writing a C
>> language function
>> employing fseek(), I just used two readBin calls: One to read (and
>> implicitly discard) data up
>> to the spot I actually wanted to read, and a second call to read the desired
>> information.
>> here is a code sample:
>> 
>>                     fileCon = file(sTsFileName,"rb")
>> #
>> # Offset has been calculated above: number of NumericByte-sized elements
>> preceeding
>> # the data of interest.
>> #
>>                     if (iOffset > 0)
>>                     {
>> 
>>                        TestVec = readBin(fileCon,numeric(),size =
>> iNumericBytes,n=iOffset)
>>                     }
>>                       dTimeSeries = readBin(fileCon,numeric(),size =
>> iNumericBytes,n=iNumElements)                     close(fileCon)
>> 
>> Crude, but effective. I WIS that we could add a seekBin() function that
>> positions the file pointer
>> to the desired spot.
>> 
> 
> The R seek function does this. I hadn't tested it when I posted,
> hoping the original poster would work it from my message. So I just
> now tested it:
> 
> I created a file with ascii a to z in, and then:
> 
>> con = file("file.tst","rb")
> # jump to 13th letter:
>> seek(con,13)
> [1] 0
> # read it:
>> readBin(con,what="raw",n=1)
> [1] 6e     # thats ascii 'n'
> # jump back:
>> seek(con,1)
> [1] 14 # returns where we were, we've now moved...
>> readBin(con,what="raw",n=1)
> [1] 62  # ascii 'b'. Things start at zero....
> 
> Is that what you are trying to do?
> 
> Barry



More information about the R-sig-Geo mailing list