Writing floats to a binary file in C++ | Equivalent of Java's DataOutputStream.writeFloat() -


i'm porting code java c++ , need write floats binary file. in java use dataoutputstream.writefloat(). how do in c++?

i tried method doesn't work:

std::ofstream out; out.open(somepath, std::ios::out | std::ios::binary);  float f = 0.5; out.write(reinterpret_cast<const char*>(&f), sizeof(float)); 

java's default behaviour write files (and other data streamed io) in big endian. c++ writes files in native format. on standard x86-derived pc native little endian, 0.5 get:

0x00 00 00 3f in c++

and

0x3f 00 00 00 in java.

in order write files can read both, have establish endian output files use , 1 of programs have reverse endian.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -