Problems reading a binary file.

This is a discussion on Problems reading a binary file. within the C++ Programming forums, part of the General Programming Boards category; hey, I have a very weird problem with trying to read in a binary file. it contains data in a ...

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    Problems reading a binary file.

    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.
    here's the main read function:
    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.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 04-27-2009, 05:56 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21