# Problems reading a binary file.

• 07-08-2005
earth_angel
hey,

I have a very weird problem with trying to read in a binary file. it contains data in a matrix format. the number of rows and columns varies. So say in a given file you have 4 rows and 3columns. At the beginning of each row there is a flag that shows which positions in the row are filled. So the whole file would look something like this:
Code:

```Flag: 101  data: 1    3 Flag: 111 data:  1 2 3 flag:  001 data:        3 flag:  010 data:    2```
when I read it, it would read and interprit the first two rows properly and the others it screws up.
Code:

```int FlagBits, FlagBytes;         unsigned char *Flag, *BinFlag;         FlagBits = (vars.NumColumns/32)*32 + 32;  // Flag size in bits         FlagBytes = FlagBits/8;  // Flag size in bytes                 Flag = new unsigned char[FlagBytes];         BinFlag =new unsigned char[FlagBits];         long Temp; float Value; int q, k, j, p=0, a,b;         unsigned int num;         float **mtxVicVals;         mtxVicVals = new  float*[vars.NumScan];         for(i=0; i<vars.NumRows; i++)                 mtxVicVals[i] = new float[vars.vectParmSelected.size()];         for (i=0; i<vars.NumRows; i++)  // -> mega loop to go down rows, i= number of scan         {                 for (j=0; j< FlagBytes; j++) // Reads in the flag                     fread(&Flag[j],sizeof(unsigned char), 1, inp);                 k=0; // bin flag position counter                 for (q=0 ; q<FlagBytes ; q++) // Flag is converted to binary                 {                            // in correct order for interpritation                                                                         num = Flag[q];                                                                         for (j=0;j<8;j++)                         {                                                 a = num/2;                         b = num%2;                         if (b == 0)                                         BinFlag[k] = 0;                         else                                         BinFlag[k]=1;                                 num = a;                                 k++;                         }                                 }                 for (j=0;j<vars.NumColumns; j++)  // -> Second big loop to go across columns,                 {                         if (BinFlag[j] == 1)  // checks if the parameter is on in the flag                         {                        // and scans in the value                                 fread(&Temp, sizeof(long), 1, inp);                                 fprintf(out, "temp = %d ", Temp);                             Temp = ((Temp & 0xFFFF) << 16) | ((Temp & 0xFFFF0000) >> 16);                             Value = (*(float*)&Temp) /4; /*if(Mask[j] == 1)                                 {                                         mtxVicVals[i][p] = Value;                                         p++;                                 }*/                                         }                         else if (BinFlag[j] ==0)                                 Value = -99; /*else if (Mask[j] == 1 && BinFlag[j] == 0)                         {                                 mtxVicVals[i][p] = -9999;                                 p++;                         }*/                                                 fprintf(out, "Val = %f  ", Value);                         Value = 0;                                         }                 fprintf(out, "\n\n======================\n\n");         }         for(i=0; i<vars.NumRows; i++)                 delete mtxVicVals[i];         delete mtxVicVals;                delete Flag;         delete BinFlag;```
Sorry it's so long.

also the mtxVicVals, the matrix that's commented out, it gives me a runtime error that memory could not be written.

The input file I provided is an example, I actually work with much bigger files and stops working at different points in the matrix.
• 07-08-2005
Salem
There sure seems to be a lot of difficulty telling the difference between C and C++ at the moment
> mtxVicVals = new float*[vars.NumScan];
Be assured this is most definitely C++ (moved)

> for (i=0; i<vars.NumRows; i++) // -> mega loop to go down rows, i= number of scan
Try doing less in the function - like split it into two separate functions
- one to read the file
- one to process the data.
• 07-08-2005
earth_angel
I know it's C++ memory allocation. I was just using C I/O.

and I can't read the file without processing it. I have to know what the flag represents to be able to read the data only into the matrix cells that are on in the flag, otherwise it won't be read properly.