[R-sig-Geo] reading select values from a binary file
Barry Rowlingson
b.rowlingson at lancaster.ac.uk
Wed Dec 9 19:33:20 CET 2009
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