Length of float changes between 32 and 40 bit

后端 未结 2 1751
你的背包
你的背包 2021-01-07 17:13

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

相关标签:
2条回答
  • 2021-01-07 17:47

    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.

    0 讨论(0)
  • 2021-01-07 17:58

    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.

    0 讨论(0)
提交回复
热议问题