Sign in to follow this  
flounder

writing floating points to file

Recommended Posts

flounder    100
i've been trying to find out how to write floating point numbers to a file. for example 32.5555 would be converted to bytes 213, 56, 2, 66. all that i've found is "IEEE" stuff, but nothing that tells me how to calculate each byte.

Share this post


Link to post
Share on other sites
dave    2187


#include <fstream>

int main( int argc, char* argv[] )
{
ofstream outputFile("path");

float number = 1.33334234f;

outputFile << number;

return 0;
}



That should work.

ace

Share this post


Link to post
Share on other sites
flounder    100
i don't know if that does work or not, but i'm still asking how you would calculate the bytes for it. i need to do more than just write to the file.

Share this post


Link to post
Share on other sites
ajas95    767

union f_to_c {
struct {
char c[4];
}
float num;
}
float num = 32.5555f;
f_to_c convert;
convert.num = num;

FILE* fp = fopen("output.dat", "wb");
fwrite(&num, sizeof(num), 1, fp);

fputc(convert.c[0], fp);
fputc(convert.c[1], fp);
fputc(convert.c[2], fp);
fputc(convert.c[3], fp);


should work... definitely verify this though.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Not sure if this works, but is how I'd start playing around. Note that it assumes the number is less then 10.


float fTemp = 1.2345;
int single;

while( fTemp != 0 )
{
single = (int) fTemp; // Hopefully gets the first number. (1)
fTemp %= 10; // Then removes it. (fTemp=0.2345)
fTemp *= 10; // And shuffles the decimal point. (2.345)
// Use single here for whatever you want.
}


Watch for rounding errors, also no idea if modulus (%=) works with floats. Other possibility is to convert it to a string.


char buf[10];
int loopC, digit;
sprintf( buf, "%f", 1.2345 );

for( loopC = 0; buf[loopC] != '\0'; loopC++ ) // Standard char array loop.
{
if( buf[loopC] == '.' ) // You've reached the decimal point, needs different treatment then the numbers.
digit = PERIOD; // Whatever PERIOD is, don't know offhand.
else
digit = buf[loopC]; // Gets the digit as a char.
// digit = buf[loopC] - '0'; This one would turn the char '0' into the int 0, etc.
// Here you'd do whatever you want with digit.
}


I'm sure someone can suggest something with no risk of buffer overflows.

Share this post


Link to post
Share on other sites
yzq89    122
Quote:
Original post by ajas95

union f_to_c {
struct {
char c[4];
}
float num;
}
float num = 32.5555f;
f_to_c convert;
convert.num = num;

FILE* fp = fopen("output.dat", "wb");
fwrite(&num, sizeof(num), 1, fp);

fputc(convert.c[0], fp);
fputc(convert.c[1], fp);
fputc(convert.c[2], fp);
fputc(convert.c[3], fp);


should work... definitely verify this though.


You have to change it to unsigned char, and it will work.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Changing char to unsigned char is unnecessary unless you are going to convert these into int/long/etc.

2flounder, just curious: why do you need that (I mean, byte representation of float/double)?

Share this post


Link to post
Share on other sites
Melekor    379
Whoa people, slow down there... :)

float the_float = 5.0f;
fwrite(&the_float, sizeof(float), 1, file);

Simple.
Add byteswapping if you need to be crossplatform.

Share this post


Link to post
Share on other sites
yzq89    122
Quote:
Original post by Anonymous Poster
Changing char to unsigned char is unnecessary unless you are going to convert these into int/long/etc.


Right. I was testing ajas95's union idea with printf, whereas he was using fputc for output.[embarrass]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this