mira
mira

Reputation: 64

Read binary file by parts

Is there a way how to read binary file into R by parts?

With readBin you can specify the number of records to be read, but is it possible to read records at specific positions?

I need to read and analyze large file with limited PC memory.

Upvotes: 0

Views: 908

Answers (1)

Spacedman
Spacedman

Reputation: 94202

Use the seek() function, just as you would in a C program.

Make a test file:

> cat(LETTERS,file="letters.txt")

See what it is - upper case with space sep:

> system("cat letters.txt") # unix only
 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Open:

> con = file("letters.txt","rb")

Go somewhere and read a few:

> seek(con,3)
[1] 0
> readBin(con,"raw",10)
 [1] 20 43 20 44 20 45 20 46 20 47

Those are ASCII codes. Go somewhere else and read a few more:

> seek(con,7)
[1] 13
> readBin(con,"raw",10)
 [1] 20 45 20 46 20 47 20 48 20 49

Upvotes: 4

Related Questions