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.
我有一个大小为2的二进制文件。但是我想把所有的数据读入一个矢量,我不知道如何找到这个二进制数据的总长度。
f <- file("a.bin", "rb")
readBin(f, integer(), size = 2, n = ??)
2 个解决方案
#1
2
Just use
只使用
file.info('a.bin')$size
#2
1
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.
用x < -扫描(“。将整个文件读入一个原始向量,然后使用y <- readBin(x, integer(), n=length(x), size=2)转换原始向量。
Each element in a raw vector is 4 bytes, so you may need to do some conversion to calculate the correct value for n
.
一个原始向量中的每个元素都是4个字节,所以您可能需要进行一些转换来计算n的正确值。
#1
2
Just use
只使用
file.info('a.bin')$size
#2
1
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.
用x < -扫描(“。将整个文件读入一个原始向量,然后使用y <- readBin(x, integer(), n=length(x), size=2)转换原始向量。
Each element in a raw vector is 4 bytes, so you may need to do some conversion to calculate the correct value for n
.
一个原始向量中的每个元素都是4个字节,所以您可能需要进行一些转换来计算n的正确值。