Reading Floats from file Streams (C++)

  • Thread starter Thread starter Alhexx
  • Start date Start date
Status
Not open for further replies.
A

Alhexx

Guest
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?
Code: [Select]
Code:
ifstream inFile( "junk" );        // open the input fileif ( !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 intfread( (char *) &y, sizeof( y ), 1, inFile ); // read a float (4-byte)fread( (char *) &z, sizeof( z ), 1, inFile ); // read a doubleofstream oFile( "crud" );          // don't forget to check that it openedif ( !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
 
Status
Not open for further replies.
Back
Top