简体   繁体   English

如何使用Tcl_ReadChars从二进制文件读取float?

[英]How to read float from binary file using Tcl_ReadChars?

The preferred method for reading from a channel using the Tcl API is Tcl_ReadChars() . 使用Tcl API从通道读取的首选方法是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() . 然后,我有一组用于从对象检索数据的函数: 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. Tcl_GetDoubleFromObj()可能有足够的灵活性来处理这种情况……但是根据文档来看,情况似乎并非如此。

Using the Tcl C API, how should I read a float from a binary file? 使用Tcl C API,我应该如何从二进制文件读取浮点数?

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 我不能发誓这是Tcl_GetDoubleFromOjb()的实现源,但看起来它可能是它的一个版本: http : //tclsrc.tyabo.com/tclObj_8c-source.html

It doesn't look like it's doing a straight conversion of a raw double. 看起来好像并没有直接转换原始double。 It looks like it's pulling a double from a TCL created object, not a stream of raw bytes. 看起来好像是从TCL创建的对象中拉取了double,而不是原始字节流。

Maybe you want to look into binary scan as a way of pulling the floats/doubles out of your binary stream? 也许您想研究binary scan ,以将浮点数/双精度数从二进制流中拉出来?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM