Re: scalar string attribute question...

NOTE: The netcdf-hdf mailing list is no longer active. The list archives are made available for historical reasons.

Ed,

You have to use
H5Tset_size (typeid,H5T_VARIABLE) instead of H5Tset_size(typeid, strlen(txt))

Elena
At 06:20 PM 9/12/2005, Ed Hartnett wrote:
Guys,

I have a question about a scalar string attribute. I create it with
the code below, and then read it back in again.

When reading it in, first I check the size, then I malloc for that
size. If I don't do this I crash into seg-fault hell.

Yet in your test program tvstr.c you read in a scalar string
attribute, which seems to be created in the exact same way, and you
somehow seem to get the HDF5 library to allocate the storage for the
string.

So how come I have to allocate storage, and you don't? I'm confused!

Thanks,

Ed

#include "tests.h"

#define FILE_NAME "tst_h_atts.h5"
#define GRP_NAME "Hamlet"
#define ATT1_NAME "Hamlets_Self_Evaluation"
#define ATT2_NAME "Commentary"

char txt[] = "O, what a rogue and peasant slave am I!\n\
Is it not monstrous that this player here,\n\
But in a fiction, in a dream of passion,\n\
Could force his soul so to his own conceit\n\
That from her working all his visage wann'd,\n\
Tears in his eyes, distraction in's aspect,\n\
A broken voice, and his whole function suiting\n\
With forms to his conceit? and all for nothing!\n\
For Hecuba!\n\
What's Hecuba to him, or he to Hecuba,\n\
That he should weep for her?";

int
main()
{
   hid_t fileid, grpid, spaceid, typeid, attid;
   char *txt_in;
   size_t txt_size;
   hssize_t size;
   size_t type_size;
   int ndims;

   printf("*** Checking HDF5 attributes attached to the fileid...");

   /* See if we can write an attribute to the root group. */
   if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT,
                           H5P_DEFAULT)) < 0) ERR;
   if ((grpid = H5Gopen(fileid, "/")) < 0) ERR;

   /* Attach a text attribute with some of Hamlet's lines. */
   if ((spaceid = H5Screate(H5S_SCALAR)) < 0) ERR;
   if ((typeid = H5Tcopy(H5T_C_S1)) < 0) ERR;
   if (H5Tset_size(typeid, strlen(txt)) < 0) ERR;
   if ((attid = H5Acreate(grpid, ATT1_NAME, typeid, spaceid,
                          H5P_DEFAULT)) < 0) ERR;
   if (H5Awrite(attid, typeid, txt) < 0) ERR;
   if (H5Aclose(attid) < 0 ||
       H5Sclose(spaceid) < 0 ||
       H5Tclose(typeid) < 0 ||
       H5Gclose(grpid) < 0 ||
       H5Fclose(fileid) < 0) ERR;

   SUMMARIZE_ERR;
   printf("*** Checking HDF5 attributes in a group...");

   /* Open file and create group. */
   if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT,
                           H5P_DEFAULT)) < 0) ERR;
   if ((grpid = H5Gcreate(fileid, GRP_NAME, 0)) < 0) ERR;

   /* Attach a text attribute with some of Hamlet's lines. */
   if ((spaceid = H5Screate(H5S_SCALAR)) < 0) ERR;
   if ((typeid = H5Tcopy(H5T_C_S1)) < 0) ERR;
   if (H5Tset_size(typeid, strlen(txt) + 1) < 0) ERR;
   if ((attid = H5Acreate(grpid, ATT1_NAME, typeid, spaceid,
                          H5P_DEFAULT)) < 0) ERR;
   if (H5Awrite(attid, typeid, txt) < 0) ERR;
   if (H5Aclose(attid) < 0 ||
       H5Sclose(spaceid) < 0 ||
       H5Tclose(typeid) < 0 ||
       H5Gclose(grpid) < 0 ||
       H5Fclose(fileid) < 0) ERR;

   /* Now open the file again and read in the attribute. */
   if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDONLY,
                        H5P_DEFAULT)) < 0) ERR;
   if ((grpid = H5Gopen(fileid, GRP_NAME)) < 0) ERR;
   if ((attid = H5Aopen_name(grpid, ATT1_NAME)) < 0) ERR;
   if ((typeid = H5Aget_type(attid)) < 0) ERR;

   /* Check the size of the string in the attribute. */
   if (H5Tget_class(typeid) != H5T_STRING) ERR;
   if (!(txt_size = H5Tget_size(typeid))) ERR;
   if (txt_size != strlen(txt) + 1) ERR;

   /* Now read the attribute. But if I don't malloc the memory first,
    * I get zapped with a seg-fault. Aren't strings supposed to be
    * different? */
   if (!(txt_in = malloc(txt_size+1))) ERR;
   if (H5Aread(attid, typeid, txt_in) < 0) ERR;


--
Ed Hartnett  -- ed@xxxxxxxxxxxxxxxx

-----------------------------------------------------------------------------------------------
Elena Pourmal, HDF QA, Maintenance and Support Team Leader
NCSA University of Illinois at Urbana-Champaign
605 E. Springfield Ave.
Champaign, IL 61820

epourmal@xxxxxxxxxxxxx
(217)333-0238 (office)
(217)244-1987 (fax)
----------------------------------------------------------------------------------------------



  • 2005 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdf-hdf archives: