I forget to mention this..
56E00: 44 3B 92 83 44 5A 6E EA 44 78 82 CF 44 32 E2 B7 "some strange letters and signs here"
56E00: 44 3B 92 83 44 5A 6E EA 44 78 82 CF 44 32 E2 B7 "some strange letters and signs here"
// write float to a binary file and read them back #include <fstream> #include <iostream> using namespace std; int main () { float test; fstream outfile; // create file with some data outfile.open ("test.dat", ios::binary | ios::out); if (! outfile) // open OK ? { cout << "\nUnable to open file test.dat " << strerror(errno); cin.get(); return 1; // open failed } // write 10 floats to the file for(int i=0; i< 10; i++) { test=i*1000.; outfile.write ((char *) &test,sizeof(float)); } outfile.close(); // now, open file for read/write outfile.open ("test.dat", ios::binary | ios::out | ios::in); if (! outfile) // open OK ? { cout << "\nUnable to open file " << strerror(errno); return 1; // open failed } // print the contents cout << "\nfile contents\n"; while(outfile.good()) { outfile.read((char *) &test,sizeof(float)); if(outfile.good()) cout << "test " << test << endl; } outfile.close(); system("pause"); return 0; }
#include <iostream> #include <iomanip> #include <fstream> using namespace std; int main() { ifstream infile; char *inname = "o1_o2.pwr1"; const int ByteNumber=8346836; int count=0; unsigned i; int length; float x; unsigned char *cXpoint = (unsigned char *)&x; infile.open(inname, ios::binary); if (!infile) { cout << "There was a problem opening file " << inname << " for reading." << endl; return 0; } cout << "Opened " << inname << " for reading." << endl; // get length of file: infile.seekg (0, ios::end); length = infile.tellg(); infile.seekg (0, ios::beg); cout << "The file's length has been determined. \nLength = The number of bytes in the file = " << length << endl; int j = 0; infile.seekg (ByteNumber); /// while (!infile.eof()) // { infile.read((char *)&x, sizeof(float)); if (!infile) { cout << "There was a problem reading " << sizeof(float) << " bytes from " << inname << endl; return 0; } cout << "The sizeof(float) value is: " << sizeof(float) << endl; cout << "The value of x is: " << x << endl; cout << "Successfully read a float from the file." << endl; cout << "The count of word now is: " << count << endl; cout << "The bytes of x in memory : "; for (i = 0; i < sizeof(float); i++) { cout << "0x" << hex << setfill('0') << setw(2) << (int)cXpoint[i] << " "; count++; } cout << endl; cout << "x = " << fixed << setprecision(6) << x << endl << endl; // } return 0; }
union data { unsigned char ch[4]; float x; }; int main(void) { data a; a.x= 1.359e+4; cout << a.x << endl; for(int i=0; i<4;i++) cout << hex << (int)a.ch[i] << " ";
union data { unsigned char ch[4]; float x; }; int main(void) { data a; a.x= 1.359e+4; cout << a.x << endl; for(int i=0; i<4;i++) cout << hex << (int)a.ch[i] << " ";
Comment