• Advertisement

Archived

This topic is now archived and is closed to further replies.

Float to file, and back again?

This topic is 5039 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I can''t seem to figure out how to write a float to file. So it is still memory archtechture independant. I want it be get copied to and from a unsigned char buffer. I don''t want, ofstream << somefloat. ect... I''m just wondering how I would do that. I tried using a reinterpt cast, but the compiler complians that I don''t have to do that, and I should be using a static_cast. Thanks, Nick

Share this post


Link to post
Share on other sites
Advertisement
Well, you should be using static_cast, reinterpret_cast is used for pointer type conversion, typically.

Share this post


Link to post
Share on other sites
// write
fprintf( "%f" );

// read
float foo = 0;
fscanf( "%f", &foo );


Tony

Share this post


Link to post
Share on other sites
To print the four bytes of a float in binary mode to a file, do:

fileout.write(reinterpret_cast<char *>(&myfloat), sizeof(float));

and to read it back in, do:

filein.read(reinterpret_cast< char *>(&myfloat), sizeof(float));

Or, if you just want to get the 4 raw bytes, do:

union
{
 unsigned char b[4];
 float f;
}

f = myfloat; // After this, b[4] contains the 4 bytes of myfloat


Or, to convert the 4 bytes to an integer value, simply do:

int myint = *reinterpret_cast<int *>(&myfloat);

Share this post


Link to post
Share on other sites
quote:
Original post by Aprosenf
To print the four bytes of a float in binary mode to a file, do:

fileout.write(reinterpret_cast<char *>(&myfloat), sizeof(float));

and to read it back in, do:

filein.read(reinterpret_cast< char *>(&myfloat), sizeof(float));


Yeah, but he''ll also have to decide on a file endian format as he wanted it to be platform independant. This means he''ll perhaps need an endian-swap function on one of the platforms he''ll be using.

You''ve still got to be careful however because different platforms, and even just different processors can produce ever so slightly different results. I''ve heard of a program that used this for replays and the replays didn''t play back the same on all machines. Given that you''re using floats not doubles, hopefully accuracy isn''t terribly important to you.

Share this post


Link to post
Share on other sites
What would I do for a double as well, would I do? Would I just use another union. Will that work well?
Thanks so far,


Nick
Müsli Games
www.musli.tk

Share this post


Link to post
Share on other sites

float value = whatever;
ofstream out;
out.open("filename", ios::binary);
out.write((char*)&value, sizeof(value));
out.close();
.
.
.

ifstream in;
in.open("filename", ios::binary);
in.read((char*)&value, sizeof(value));
in.close();




"A soldier is a part of the 1% of the population that keeps the other 99% free" - Lt. Colonel Todd, 1/38th Infantry, Ft. Benning, GA

Share this post


Link to post
Share on other sites

  • Advertisement