-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Variable length string Read not working #6
Comments
I can reproduce this issue. I create a datset with a simple 1-D dataspace, with datatype dataspace, _ := hdf5.CreateSimpleDataspace([]uint{1}, nil)
dataset, _ := file.CreateDataset("group/xml", hdf5.T_GO_STRING, dataspace)
wrapper := []string{xml}
dataset.Write(&wrapper) This works great! However, when I want to read my string back from the dataset I get an empty string: dataset, _ := file.OpenDataset("group/xml")
wrapper := make([]string, 1)
dataset.Read(&wrapper)
fmt.Println(wrapper[0]) |
In fact, the
I'm not positive, but, at least for strings and string slices, I don't think you can just call diff --git a/h5d.go b/h5d.go
index a3fa9d9..f22059e 100644
--- a/h5d.go
+++ b/h5d.go
@@ -149,6 +149,20 @@ func (s *Dataset) WriteSubset(data interface{}, memspace, filespace *Dataspace)
filespace_id = filespace.id
}
rc := C.H5Dwrite(s.id, dtype.id, memspace_id, filespace_id, 0, addr)
+
+ // only checking for string slices
+ sp, ok := data.(*[]string)
+ if ok {
+ // show that the string was actually read from the file
+ fmt.Print("C.puts(...): ")
+ C.puts(*(**C.char)(addr))
+
+ // make Go believe it's a string of len > 0
+ head := (*reflect.SliceHeader)(unsafe.Pointer(&(*sp)[0]))
+ head.Len = int(C.strlen((*C.char)(unsafe.Pointer(head.Data))))
+ fmt.Println("s[0]: ", (*sp)[0])
+ }
+
err = h5err(rc)
return err
} This doesn't work, for example, with a |
Read() of scalar variable length string data does not appear to work.
var buf string
dset.Read(buf, hdf5.T_GO_STRING)
will error with:
panic: reflect.Value.UnsafeAddr of unaddressable value [recovered]
panic: reflect.Value.UnsafeAddr of unaddressable value
using dset.Read(&buf, hdf5.T_GO_STRING) works with uint, etc. but not with string. There is not error but buf will be empty.
The text was updated successfully, but these errors were encountered: