Ok es liegt tatsächlich daran das die bytes falschrum gespeichert sind. Folgendes funktioniert einwandfrei:
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
#include <fstream>
#include <iostream>
using namespace std;
int main(int argc, char** args){
// schreiben
ofstream file;
file.open("test.bin", ofstream::binary);
int value = 22;
file.write(reinterpret_cast<char*>(&value), 4);
file.close();
// lesen
ifstream ifile;
ifile.open("test.bin", ifstream::binary);
int ivalue;
ifile.read(reinterpret_cast<char*>(&ivalue), 4);
cout<<ivalue<<endl; // 22
while(true){}
return 0;
}
|
Im Hex Editor steht 16 00 00 00
Aber muss ich die bytes jetzt echt von Hand umdrehen oder geht das auch anders?
[EDIT]
Hässliche Möglichkeit die funktioniert:
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
|
char c[4];
file.read(reinterpret_cast<char*>(&c), sizeof(c));
char f[4];
f[0] = c[3];
f[1] = c[2];
f[2] = c[1];
f[3] = c[0];
int i = *reinterpret_cast<int*>(&f);
cout<<i<<endl;
|
Geht das nicht eleganter?