Hi all,
I'm having a trouble reading a binary file as float values I have to read the data in binary mode, then read every four bytes into a float variable. I have done my search, but i found out that the read and get functions do not accept float type. What should i do now? Can anybody help me please?
Here is the code..
Code:// read a file into memory #include <iostream> #include <fstream> using namespace std; int main () { int length; float * buffer; //open the file ifstream is; is.open ("o1_o2.pwr1", ios::binary ); cout << "The file is opened.." << endl; // get length of file: is.seekg (0, ios::end); length = is.tellg(); is.seekg (0, ios::beg); cout << "The file's length has been determined.." << endl; // allocate memory: buffer = new float [length]; cout << "The memory is allocated.." << endl; // read data as a block: // is.read (buffer,length); is.read ((char *)buffer, sizeof(float)*length); is.close(); cout << "Reading data as a block.." << endl; cout << "Writing data.." << endl; // cout.write (buffer,length); cout.write ((char *)buffer, sizeof(float)*length); delete[] buffer; return 0; }
and i get outout like this:
قEذج╧Fd╟F♂EفYزEءQ3FcJmEاgdE┘≈G>ىF17hEَ7FX╨ F☻ؤF]P{E~h█F♦♣Eَ╩حF∟↨F>@┬F>\Ex╤
(E╢صF♦╟E╪YٌFpفF░╫E═z Eد╫F MF►ه
F►QDF@═FCsإEdE┘╕F#♫F.k║F♠IEuHE\╜▀F☻↓7Eغ8 pEٍO RFْ♣6Fb↨╜E╕╕_F*Z+FjإE┐▲'F(eيE
■/EWT}Eآ~XEْ♥F=→يEأ♫SEإ5E/qF <╔F@ُ:F!<←F#9╔Eى┘╖ENٍE╫Z╙E┼خ╒F☻P>EسZ#E╥آ[EdءْE
which is what i get when i open the file using Notepad, so i guess it need to be converted to a float type. But, when i opened the file in Binary mode using TextPad, i got numbers like this:
56E00: 44 3B 92 83 44 5A 6E EA 44 78 82 CF 44 32 E2 B7
The thing is, how can i do this according to my code, knowing that the values i should get are float values?