HDF5中保存包含固定长度和可变长度字符串的DataSet

#include <hdf5.h>

const char* HOLDINGSET = "Holding";
const int RECORDCOUNT = 2;
const int CUSIPLEN = 9;

/* First structure  and dataset*/
typedef struct {
 char   *HoldingName;
 int    Shares;
 double MarketValue;
 char   CUSIP[CUSIPLEN];
} Holding;

void WriteHDF5Exam(const char* fileName)
{
 hid_t      file, dataset, space;  // Handles
 herr_t     status;
 hsize_t    dim[] = { RECORDCOUNT };    // Dataspace dimensions

 // Initialize the data
 Holding s1[RECORDCOUNT] = {
  { "MSCI", 1000, 42134, "ddd" },
  { "JP Morgan", 30, 3900, "ffff" }
 };
 //for (int i = 0; i< LENGTH; i++) {
 // s1[i].HoldingName = (char*)malloc(100);
 // sprintf_s(s1[i].HoldingName, 100, "Holding Name: %d", i);

 // s1[i].Shares = i;
 // s1[i].MarketValue = 1./(i+1);

 // sprintf_s(s1[i].CUSIP, CUSIPLen, "CUSIP:%d", i);
 //}

 // Create the data space
 space = H5Screate_simple(1, dim, NULL);

 // Create the file
 file = H5Fcreate(fileName, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

 // Create the memory datatype
 hid_t t_str = H5Tcopy(H5T_C_S1);
 H5Tset_size(t_str, CUSIPLEN);

 hid_t t_string = H5Tcopy(H5T_C_S1);
 H5Tset_size(t_string, H5T_VARIABLE);

 hid_t holding_tid = H5Tcreate (H5T_COMPOUND, sizeof(Holding));
 H5Tinsert(holding_tid, "HoldingName", HOFFSET(Holding, HoldingName), t_string);
 H5Tinsert(holding_tid, "Shares", HOFFSET(Holding, Shares), H5T_NATIVE_INT);
 H5Tinsert(holding_tid, "MarketValue", HOFFSET(Holding, MarketValue), H5T_NATIVE_DOUBLE);
 H5Tinsert(holding_tid, "CUSIP", HOFFSET(Holding, CUSIP), t_str);

 // Create the dataset.
 dataset = H5Dcreate(file, HOLDINGSET, holding_tid, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

 // Write data to the dataset
 status = H5Dwrite(dataset, holding_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, s1);

 // Release resources
 H5Tclose(holding_tid);
 H5Sclose(space);
 H5Dclose(dataset);
 H5Fclose(file);
}

void ReadHDF5Exam(const char* fileName)
{
 hid_t      file, dataset, space;  // Handles
 herr_t     status;
 int i;

 typedef struct {
  double c;
  int    a;
 } s2_t;
 s2_t       s2[RECORDCOUNT];
 hid_t      s2_tid;

 typedef struct {
  char   CUSIP[9];
 } s3_t;
 hid_t      s3_tid;
 s3_t       s3[RECORDCOUNT];

 typedef struct {
  char* HoldingName;
 } s4_t;
 hid_t     s4_tid;
 s4_t      s4[RECORDCOUNT];

 // Open the file and the dataset
 file = H5Fopen(fileName, H5F_ACC_RDONLY, H5P_DEFAULT);
 dataset = H5Dopen(file, HOLDINGSET, H5P_DEFAULT);

 s2_tid = H5Tcreate(H5T_COMPOUND, sizeof(s2_t));

 H5Tinsert(s2_tid, "MarketValue", HOFFSET(s2_t, c), H5T_NATIVE_DOUBLE);
 H5Tinsert(s2_tid, "Shares", HOFFSET(s2_t, a), H5T_NATIVE_INT);

 status = H5Dread(dataset, s2_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, s2);

 printf("\n");
 printf("Field c : \n");
 for(i = 0; i < RECORDCOUNT; i++) printf("%.4f, ", s2[i].c);
 printf("\n");

 printf("\n");
 printf("Field a : \n");
 for( i = 0; i < RECORDCOUNT; i++) printf("%d, ", s2[i].a);
 printf("\n");

 hid_t t_str = H5Tcopy(H5T_C_S1);
 H5Tset_size(t_str, CUSIPLEN);

 s3_tid = H5Tcreate(H5T_COMPOUND, sizeof(s3_t));
 status = H5Tinsert(s3_tid, "CUSIP", 0, t_str);

 status = H5Dread(dataset, s3_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, s3);

 printf("\n");
 printf("Field b : \n");
 for( i = 0; i < RECORDCOUNT; i++) printf("%s, ", s3[i].CUSIP);
 printf("\n");


 hid_t t_string = H5Tcopy(H5T_C_S1);
 H5Tset_size(t_string, H5T_VARIABLE);

 s4_tid = H5Tcreate(H5T_COMPOUND, sizeof(s4_t));
 status = H5Tinsert(s4_tid, "HoldingName", 0, t_string);

 status = H5Dread(dataset, s4_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, s4);

 printf("\n");
 printf("Field b : \n");
 for( i = 0; i < RECORDCOUNT; i++) printf("%s, ", s4[i].HoldingName);
 printf("\n");


 // Release resources
 H5Tclose(s2_tid);
 H5Tclose(s3_tid);
 H5Dclose(dataset);
 H5Fclose(file);
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值