如何使用R从二进制文件读取浮点数据

时间:2021-10-05 22:57:09

I know there is no float in R. so How can I read float data from a binary file. the struct of data in C as follows

我知道r中没有浮点数,所以我如何从二进制文件中读取浮点数。C中的数据结构如下

typedef struct
{
    int date;
    int open;
    int high;
    int low;
    int close;
    float amount;
    int vol;
    int reservation;
} StockData; 

to.read = file(filename, "rb");
line1=readBin(to.read,  "int",8);

amount is not the right value. how can I get the right value as float?

金额不是正确的值。如何获得浮点数的正确值?

1 个解决方案

#1


3  

Your C structure is made of 5 integer values followed by a float and then 2 integers again. So, you can call readBin three times:

C结构由5个整数值组成,后面跟着一个浮点数,然后又是2个整数。你可以给readBin打三次电话:

 line1<-c(readBin(to.read,"int",5), 
          readBin(to.read,"double",1,size=4),
          readBin(to.read,"int",2))

You handle the float value by setting the size argument to 4, since a float has a size of 4 bytes (instead of the 8 of a double).

您可以通过将大小参数设置为4来处理浮点值,因为float的大小是4字节(而不是double的8字节)。

#1


3  

Your C structure is made of 5 integer values followed by a float and then 2 integers again. So, you can call readBin three times:

C结构由5个整数值组成,后面跟着一个浮点数,然后又是2个整数。你可以给readBin打三次电话:

 line1<-c(readBin(to.read,"int",5), 
          readBin(to.read,"double",1,size=4),
          readBin(to.read,"int",2))

You handle the float value by setting the size argument to 4, since a float has a size of 4 bytes (instead of the 8 of a double).

您可以通过将大小参数设置为4来处理浮点值,因为float的大小是4字节(而不是double的8字节)。