Read and write 16 byte doubles from Fortran selected_real_kind in C++ (where system long double is 8 bytes)

Viewed 109

I have a binary file written by a Fortran code which I am required to read in using C++, edit, and then rewrite to the original format.

The majority of the file is straightforward to manipulate but I am encountering a problematic type. The file has been written using:

integer, parameter :: long = selected_real_kind(18,4096)

and subsequently has a number of values to be read/written of type:

real(long)

I have been able to determine that these values are stored as 16 bytes in the original binary file and hence should be read in to a long double, but on my system a long double is only 8 bytes and reads incorrectly.

Firstly, can anyone tell me anymore information about how the selected_real_kind values are stored as binary?

Secondly, can anyone recommend a way to read and write this abnormal 16 byte binary real in and out of C++ which can only store up to 8 byte doubles?

EDIT: This is all using Visual Studio 2017 on Windows 10, so GCC quadmath.h is not applicable.

0 Answers
Related