Improved error management in read/writeStruct functions