0
votes

The preferred method for reading from a channel using the Tcl API is Tcl_ReadChars(). When reading from a binary channel, the data will be stored as a byte array until referenced, at which point conversions will be done.

I then have a set of functions for retrieving data from the object: Tcl_Get{String,Double,Int}FromObj(). However, if I read in a float, it seems that none of these will do the right thing to extract the value I intended.

At this point, I am trying to reason from the documentation. It may be the case that Tcl_GetDoubleFromObj() will be flexible enough to handle this case... but that doesn't seem to be the case based on the documentation.

Using the Tcl C API, how should I read a float from a binary file?

1
At the C level? Get the bytes and directly cast them. That's what the binary command does. (Tcl_GetDoubleFromObj parses a human-readable string.) - Donal Fellows

1 Answers

1
votes

I can't swear this is the source of the implementation of your Tcl_GetDoubleFromOjb(), but it looks like it might be a version of it: http://tclsrc.tyabo.com/tclObj_8c-source.html

It doesn't look like it's doing a straight conversion of a raw double. It looks like it's pulling a double from a TCL created object, not a stream of raw bytes.

Maybe you want to look into binary scan as a way of pulling the floats/doubles out of your binary stream?