Qhimm.com Forums
Miscellaneous Forums => Scripting and Reverse Engineering => Topic started by: Alhexx on 2002-04-12 17:01:00
-
I'm workin' with a FILE type and I've used functions like fgetc, fgetw and so on to read out integers.
But how the heck can I read out a IEEE float from a binary file stream under C++ ? :-?
BTW: I'm programmin' with Win32 API, no MFC ...
- Alhexx
-
Hm ... could this be useful?
ifstream inFile( "junk" ); // open the input file
if ( !inFile.is_open() )
{
cout << "I can't open 'junk'";
return 2;
}
int x;
float y;
double z;
fread( (char *) &x, sizeof( x ), 1, inFile ); // read an int
fread( (char *) &y, sizeof( y ), 1, inFile ); // read a float (4-byte)
fread( (char *) &z, sizeof( z ), 1, inFile ); // read a double
ofstream oFile( "crud" ); // don't forget to check that it opened
if ( !inFile.is_open() )
{
cout << "I can't open 'crud'";
return 2;
}
oFile.write( (char *) &z, sizeof( z ) ); // write a double value (in binary)
oFile.close();
inFile.close();
I'll try it out :D
- Alhexx
-
Should work. But i never used that 'ifstream' thingy. This is the way how i use it, and it works in both dos and win.
FILE *fin;
float f;
fin = fopen( "this.one", "rb" ); //rb means read-binary
if ( fin != NULL ) {
fread( &f, 1, 4, fin ); //size of float = 4 bytes
fclose( fin );
}
-
Yes, I don't use it, too. This was just a source I found on the web.
- Alhexx