Re: NXC Reading and writing arrays.
Posted: 30 Nov 2010, 03:37
No, that's what ReadBytes() is for.m-goldberg wrote:I hope John Hansen will comment on this. John, should Read be able to read in an array of ints with a single call?
Give a child a robot, he'll play for an hour. Teach him to build, and he'll play forever.
https://mindboards.org:443/
No, that's what ReadBytes() is for.m-goldberg wrote:I hope John Hansen will comment on this. John, should Read be able to read in an array of ints with a single call?
ReadBytes would let you read in an array of bytes in single call but definitely not an array of ints or longs. The problem with reading in an array of anything is knowing how many bytes to read. The ReadBytes function requires that you provide that information. For structures and simple types like long, int, float, etc... the compiler knows how many bytes that type needs to have read from the file in order to unflatten the bytes into the variable provided. This operation is a firmware VM opcode which works if you give the opcode the right type as the pattern to use when converting the array of bytes containing the data read from the file into the output variable. You can flatten an array containing any type into a byte array (actually a string since it has a null added at the end) since the variable you are flattening has all the information the firmware needs such as the element type and the number of elements in the array. But when trying to read that data in from a file to which it was previously written the compiler has no idea how many elements were previously written to the file so the API function can't automatically figure that out like it can for a Read of a int or a float or a user-defined type. If a Write operation of an array included a 4 byte header (by convention) containing the number of bytes being written then a Read of an array could use that to figure out the mystery and solve it.muntoo wrote:No, that's what ReadBytes() is for.m-goldberg wrote:I hope John Hansen will comment on this. John, should Read be able to read in an array of ints with a single call?