writing floating points to file

Started by
7 comments, last by yzq89 18 years, 10 months ago
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.
Advertisement
#include <fstream>int main( int argc, char* argv[] ){  ofstream outputFile("path");  float number = 1.33334234f;     outputFile << number;  return 0;}


That should work.

ace
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.
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.
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.
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.
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)?
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.
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]

This topic is closed to new replies.

Advertisement