I did a post on comp.lang.c++ and got this
but that is still not the answer.
I have a little confusion for a binary read operation.
I am trying to read a binary file with the stream functions. This is a result file of a commercial program(ANSYS), and I know the structure of the file, at least from the manual.
The file is structured as records and the program is written in fortran. So the structure is like
record Length (int) dummy integer data (could be int, double) dummy integer
The first record is a 100 integer block, where this corresponds to data in the above representation.
If I start reading the file and read the first value which is the record length (an integer), I have to swap the bytes to get the correct value of 100.
I did not understand why I have to swap the bytes, because this file is generated on the same machine and they should be using the same system specific routines so that should not be a problem, but it does not seem that is the case. There is sth else going on. I could not understand this. Can the software be forcing to swap the bytes which I would have hard time to understand the reason?
Any comments are appreciated.
Here is a naive test case
int main () {
ifstream myfile;
char intBuffer[4];
myfile.open ("truss.rst", ios::binary);
myfile.read(intBuffer, sizeof(int));
//cout << *((int*)intBuffer) << endl;
// if I do not use this portion-
// I do not get what I want
char *cptr, tmp;
tmp = intBuffer[0];
intBuffer[0] = intBuffer[3];
intBuffer[3] = tmp;
tmp = intBuffer[1];
intBuffer[1] = intBuffer[2];
intBuffer[2] = tmp;
// -----------------------------
cout << *((int*)intBuffer) << endl;
myfile.close();
return 0;
}
Best, U.