Ikram Ikram - 1 month ago 5x
C++ Question

cryptic hexadecimal character printed to file by c++ ofstream

I have a C++ implementation for a species tree inference algorithm (https://bitbucket.org/ikramu/mixtreem/). I used the program to generate the results (last used Nov 2015). Recently, a friend complained that the program writes cryptic hexadecimal characters with species tree to the file. When I checked, it turns out that the cout functionality is working perfectly OK but the ofstream object, while writing the same output to file, prefixes it with hex characters.

Here is an example tree string in wrong format


Here is a tree string in correct format


The actual line number is 202 at https://bitbucket.org/ikramu/mixtreem/src/425d7be0b17b57ca29b15804db3ee84df9b53bd0/applications/mixtreem/StisemLog.cpp#StisemLog.cpp-202

As I have been away from C/C++ for more than a year, I don't know if it is due to some modifications in platform, C++ version, architecture or something else? The code has not been changed since (I compiled and tested older versions of the code as well). Will be grateful for Any ideas/hints?

I think it has something to do with the way double values are (ill)printed. Please see the attached figure from debugging session. While the value Netbeans show in debugger is -12315.334990286983, the printed value (in Output in the bottom) is -0x1.80daae0f63389p+13.
enter image description here


Yes it looks like there is hexadecimal in the results but it looks like it is an output formatting issue in sp_tree.output function. Add that code to the question.

Note the each value ends with one of: p-1, p-2, p-3, p-4, p-5, p-6 and neither p or - is a hexadecimap character. Best bet is the specified format can not hangle the data values and defaults to output you are seeing.

See C++ printf and you will see that the format you are seeing is due to a formatting character: 'a'.

From the documentation:

specifier Output                                 Example
a         Hexadecimal floating point, lowercase  0xc.90fep-2

Compare to your output: 0x1.d517c27fc6fe9p-1

Perhaps there is a dynamic format that is causing this output.