Shicheng Guo
Shicheng Guo

Reputation: 1293

How to find the length of the binary file

I have a binary file with size=2. But I want to read all the data into a vector and I don't how to find the total length of this binary data.

f <- file("a.bin", "rb")
readBin(f, integer(), size = 2, n = ??)

Upvotes: 4

Views: 3078

Answers (2)

eddi
eddi

Reputation: 49448

Just use

file.info('a.bin')$size

Upvotes: 3

Joshua Ulrich
Joshua Ulrich

Reputation: 176698

Use x <- scan("a.bin", raw()) to read the entire file into a raw vector, then use y <- readBin(x, integer(), n=length(x), size=2) to convert the raw vector.

Each element in a raw vector is 4 bytes, so you may need to do some conversion to calculate the correct value for n.

Upvotes: 1

Related Questions