cherva
February 22nd, 2009, 07:15 PM
Can someone write a simple code to show me how to convert a binary file to an integer matrix in C++ ? I know I should open the file in binary mode, but then what ? I need to get somethin like this:
int file2[29312] = {77,90,-112,0,3,0,0,0,4,0,0,0,-1,-1,0,0,-72,0,0,0,0,0,0,0,64,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,-72,0,0,0,
14,31,-70,14,0,-76,9,-51,33,-72,1,76,-51,33,84,104,105,115,32,112,114,111,103,11
4,97,109,32,99,97,110,110,111,116,32,98,101,32,114 ,117,110,32,105,110,32,68,79,8
3,32,109,111,100,101,46,13,13,10,36,0,0,0,0,0,0,0,-43,90,-115,-39,-111,59,-29,-1
18,-111,59,-29,-118,-111,59,-29,-118,-111,59,-29,-118,-120,59,-29,-118,41,61,-27
,-118,-112,59,-29,-118,121,36,-23,-118,-105,59,-29,-118,82,105,99,104,-111,59,-2
9,-118,0,0,0,0,0,0,0,0,80,69,0,0,76,1,5,0,49,-54,102,57,0,0,0,0,0,0,0,0,-32,0,14
,1,11,1,6,0,32,92,0,0,-32,19,0,0,0,0,0,0.......................
so I can put it in a separate .h file and then when my program is runed to construct the file back to its original state....
I suppose the reconstructing is done like this:
void write(char filename[], int size, int file[])
{
ofstream outfile (filename,ofstream::binary | ios::out);
for (int i=0; i<size; i++) {
outfile << (char)file[i];
}
outfile.close();
}
int file2[29312] = {77,90,-112,0,3,0,0,0,4,0,0,0,-1,-1,0,0,-72,0,0,0,0,0,0,0,64,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,-72,0,0,0,
14,31,-70,14,0,-76,9,-51,33,-72,1,76,-51,33,84,104,105,115,32,112,114,111,103,11
4,97,109,32,99,97,110,110,111,116,32,98,101,32,114 ,117,110,32,105,110,32,68,79,8
3,32,109,111,100,101,46,13,13,10,36,0,0,0,0,0,0,0,-43,90,-115,-39,-111,59,-29,-1
18,-111,59,-29,-118,-111,59,-29,-118,-111,59,-29,-118,-120,59,-29,-118,41,61,-27
,-118,-112,59,-29,-118,121,36,-23,-118,-105,59,-29,-118,82,105,99,104,-111,59,-2
9,-118,0,0,0,0,0,0,0,0,80,69,0,0,76,1,5,0,49,-54,102,57,0,0,0,0,0,0,0,0,-32,0,14
,1,11,1,6,0,32,92,0,0,-32,19,0,0,0,0,0,0.......................
so I can put it in a separate .h file and then when my program is runed to construct the file back to its original state....
I suppose the reconstructing is done like this:
void write(char filename[], int size, int file[])
{
ofstream outfile (filename,ofstream::binary | ios::out);
for (int i=0; i<size; i++) {
outfile << (char)file[i];
}
outfile.close();
}