gpt4 book ai didi

c - HDF5:更新整数表中的单元格

转载 作者:行者123 更新时间:2023-11-30 17:30:52 25 4
gpt4 key购买 nike

注意:交叉发布于 http://mail.lists.hdfgroup.org/pipermail/hdf-forum_lists.hdfgroup.org/2014-July/007960.html

使用 hdf5,我构建了一个 5x4 表。表格的每个单元格包含 3 个整数我正在尝试编写一个函数 setyx 来更新位于 y,x 处的表格单元格这是我到目前为止编写的 C 代码。

#include "hdf5.h"

/** set 3 'int' values at y/x */
static void setyx(hid_t dataset, hid_t dataspace,
int y,int x,
int i1,int i2,int i3)
{
int values[3]={i1,i2,i3};
hsize_t starts[3]={y,x,0};
hsize_t counts[3]={1,1,1};
hsize_t dims_memory[3]={1,1,3};

H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, starts, NULL, counts, NULL);
hid_t filespace = H5Dget_space(dataset);
hid_t memspace = H5Screate_simple(3, dims_memory, NULL);

H5Dwrite(dataset,
H5T_NATIVE_INT,
memspace,
filespace,
H5P_DEFAULT,
values);

H5Sclose(memspace);
H5Sclose(filespace);
}


int main (void)
{
hid_t file, dataset;
hid_t dataspace;
hsize_t dims[3]={5,4,3};


file = H5Fcreate("test.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
dataspace = H5Screate_simple(3, dims, NULL);
dataset = H5Dcreate(file, "/test", H5T_NATIVE_INT, dataspace,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
setyx(dataset,dataspace,2,1, 100,200,300);

H5Sclose(dataspace);
H5Dclose(dataset);
H5Fclose(file);
return 0;
}

但它会引发一些错误:

  #000: H5Dio.c line 225 in H5Dwrite(): can't prepare for writing data
major: Dataset
minor: Write failed
#001: H5Dio.c line 347 in H5D__pre_write(): can't write data
major: Dataset
minor: Write failed
#002: H5Dio.c line 685 in H5D__write(): src and dest data spaces have different sizes
major: Invalid arguments to routine
minor: Bad value

更新 (y,x) 处给定单元格的正确方法是什么?

最佳答案

该问题已在 HDF5 邮件列表上得到解答:http://mail.lists.hdfgroup.org/pipermail/hdf-forum_lists.hdfgroup.org/2014-July/007966.html

这是代码:

#include <stdio.h>
#include <stdlib.h>
#include "hdf5.h"

#define VERIFY(a) do { if((a)<0) { fprintf(stderr,"Failure line %d.\n",__LINE__); exit(-1);}}while(0)

/** set 3 'int' values at y/x */
static void setyx(hid_t dataset, hid_t dataspace,int y,int x,int i1,int i2,int i3)
{
int values[3]={i1,i2,i3};
hsize_t starts[3]={y,x,0};
hsize_t counts[3]={1,1,3};

hsize_t dims_memory[3]={1,1,3};



hid_t filespace = H5Dget_space(dataset);
hid_t memspace = H5Screate_simple(3, dims_memory, NULL);
VERIFY(memspace);

VERIFY(H5Sselect_hyperslab(dataspace,
H5S_SELECT_SET,
starts, NULL,
counts, NULL));

VERIFY(H5Dwrite(dataset,
H5T_NATIVE_INT,
memspace,
dataspace,
H5P_DEFAULT,
values));

H5Sclose(memspace);
H5Sclose(filespace);
}


int main (void)
{
hid_t file, dataset;
hid_t dataspace;
hsize_t dims[3]={5,4,3};


file = H5Fcreate("test.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
dataspace = H5Screate_simple(3, dims, NULL);
dataset = H5Dcreate(file, "/test", H5T_NATIVE_INT, dataspace,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
setyx(dataset,dataspace,2,1, 100,200,300);

H5Sclose(dataspace);
H5Dclose(dataset);
H5Fclose(file);
return 0;
}

关于c - HDF5:更新整数表中的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24883461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com