0

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
  • 2
    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.) Commented Sep 8, 2015 at 5:41

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for pointing me to binary scan. Although if the solution involves hacking at the internals of the Tcl_Obj, then it's probably better to just read the data without using the Tcl API at all.
I think binary scan is part of the TCL language. No need to hack anything. I was just showing the implementation of Tcl_GetDoubleFromObj() to point out it might not be what you're looking for.
Ah, sorry, my mistake. I'm still feeling my way around these tools. The more I've read and experimented over the course of the night, I'm pretty sure that I'm asking for something that has a lot more hidden complexity than I thought at first. Thanks for helping out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.