Years ago I wrote a program that created binary files. Now I can't read the file. |
A number of things could have changed since then and now. For example:
• size of fundamental types such as
int
may have changed
• endian-ness of hardware may be different (byte order)
• packing options for the struct may differ
As a first attempt at diagnosing the nature of the problem, find the exact length in bytes of the file. It should be an exact multiple of the size in bytes of the struct.
Depending on the whether or not there are padding bytes, I get a size for the struct
materials
of either 90 or 92 bytes. That is based on a float and int each needing 4 bytes.
The padding, well
1 2
|
char A[7];
char B[27];
|
together make a total of 34 bytes. The compiler may try to align the float or int on a boundary which is a multiple of 4 bytes, so there may or may not be an extra two bytes padding inserted before the start of
float C;
How do I attach the "rawmat'l.stk" file? |
You could upload it to some external file-sharing service, and post the link here.
Or, as a partial alternative (ok for small files), you could run it through some code to convert it to hexadecimal values as readable text and post the output here. A quick example:
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 <iostream>
#include <fstream>
#include <cstdio>
using namespace std;
const char* hex(unsigned char ch)
{
static char hexbuf[4] = {0};
sprintf(hexbuf, "%02X", ch);
return hexbuf;
}
int main()
{
// read the binary file and output as text
const char * file_name = "c:/x_rm2/rawmat'l.stk";
const char * output_name = "c:/x_rm2/hexadecimal.txt";
ifstream fin(file_name, ios::binary);
ofstream fout(output_name);
char ch;
for (int i=0; fin.get(ch); i++)
{
if (i>0 && i%16 == 0) fout << '\n';
fout << hex(ch) << ' ';
}
}
|
Sample output:
41 70 70 6C 65 73 00 50 65 61 72 20 2E 2E 2E 2E
2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 20 54 72 65
65 00 00 00 9A 99 99 3F 9A 99 59 40 33 33 B3 40
15 03 00 00 D0 0F 49 40 4D F8 2D 40 B0 01 00 00
0E 2D 6A 40 0A D7 12 42 66 A6 B7 43 00 D0 65 45
AD FA BC 3E 48 E1 13 42 2F DD 6C 40 |
The above program reads the file and creates an output file named
"hexadecimal.txt"
. It may be quite long, if you post at least the first dozen or so lines that should be more than enough.
The original binary file can be reconstructed like so:
1 2 3
|
std::ifstream fin("hexadecimal.txt");
std::ofstream fout("convert.bin", std::ios::binary);
for (int n; fin >> std::hex >> n; ) fout.put(n);
|