Saving a float to a binary file in C# and opening in C -


i making application required save array of float values binary file in c# , open same binary , retrieve values in c program.

i using binarywriter in c#

binarywriter.write(myfloatvar);  

and using fread in c

fread(&myfloat, sizeof(float), 1, file); 

for majority works fine, when trying fread particular value output comes out strange. input 0.6045232 on c# side found through debuging, when retrieved on c program side appears 6.961e-041#den

i know den means denormalised number, unsure how has happened or how fix it. appreciated.

a usual case fp works fine, in ms world, 1 1 of files operating in text mode , other binary.

for c side, insure file open in binary

// file *istream = fopen(fileanme, "r");  // r file *istream = fopen(fileanme, "rb");    // rb 

check c# side too.


example

int main(void) {   union {       float f;       uint32_t u32;   } u;   u.f = 0.60452329;   printf("0x%08lx %.9e\n", (unsigned long) u.u32, u.f);   return 0; } 

output

//      \n  gets converted \r\n , messes rest of file //      vv 0x3f1ac20a 6.045233011e-01 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -