I encountered an odd problem when exporting float
values to a file. I would expect every float to be of the same length (obviously), but my programme sometimes
You're not writing text; you're writing binary data... However, your file is open for writing text ("w"
) instead of writing binary ("wb"
). Hence, fwrite()
is translating '\n'
to "\r\n"
.
Change this:
if (!fopen_s(&outputFile, fileName, "w"))
To this:
if (!fopen_s(&outputFile, fileName, "wb"))
In "wb"
, the b
stands for binary mode.
The problem is that on Windows, you have to differentiate between text and binary files. You have the file opened as text, which means 0d
(carriage-return) is inserted before every 0a
(line-feed) written. Open the file like this:
if (!fopen_s(&outputFile, fileName, "wb"))
The rest as before, and it should work.