1.判断文件是否成功创建?
2.判断dataset中是否成功写入数据?
关键:通过file和dataset的open函数返回值来判断。
第一种方法:直接看看文件夹里面有没有最新生成的文件,我一般会给文件名命名加上时间戳,直观判断;
第二种方法:如果因为一些原因不方便直观判断,那么就在程序中通过代码中H5Fopen和H5Freopen的返回值来判断:
file=H5Fopen(filename,H5F_ACC_RDONLY,,H5P_DEFAULT);
if(file<0)printf("File is not found.\n");
status=H5Fclose(file);
第一种方法:也是直观方法,打开文件看一下是否有有数据;
第二种方法:在代码中通过H5Dopen2的返回值来判断:
dataset=H5Dopen2(file,"/Data/new_data",H5P_DEFAULT);
if(dataset<0)printf("Dataset is not found.\n");
status=H5Dclose(dataset);
下面介绍HDF5中的两种返回值,也是为了解释为什么要用上述的这几种函数的返回值来判断是否创建和写入成功。
herr_t类型的返回值常用于读写函数,关闭函数,数据维度拓展函数等,一般会声明一个herr_t类型的值(常用status)比如:
herr_t status;
status=H5Dwrite(dataset,H5T_NATIVE_INT,H5S_ALL,H5S_ALL,H5P_DEFAULT,data);
看一下herr_t的介绍
typedef int herr_t
Status return values.Failed integer functions in HDF5 result almost
always in a negative value (unsigned failing functions sometimes returnzero for failure)while successfull return is non-negative (often zero).
The negative failure value is most commonly -1,but don't bet on it.Theproper way to detect failure is something like:
if((dset H5Dopen2(file,name))<0)
fprintf(stderr,"unable to open the requested dataset\n");
很明显来自官方的吐槽but don't bet it!然后最后一段还给出了正确的使用方法,那么我们下面就看看正确的使用方法的返回值类型是什么样的。
看一下hid_t的官方介绍
typedef int hid_t
Type of atoms to return to users
说明这是专门用于返回给用户使用的返回类型
而H5Dopen2的返回值类型也是hid_t,所以我们通过这种类型的返回值可以更好的做一些判断。
同理对于group也可以使用H5Gopen1的返回值来判断,可以将这些检查放到try catch中进行进一步的异常检查,使得代码更加的完善。