Monday, December 12, 2011

Help me create a binary file read routine in C?

Problem:


I need to develop simple program for canny edge detector.


Now, I need help to create a routine to read a binary file. The routine must be able to read a binary file and pass these values:


a) first 2 bytes as unsigned integer (assign it to "row" variable)


b)next 2 bytes as unsigned integer (assign it "col" variable)


skip 6 bytes


e) a 2 dimensional array ( data[row][colum] ) from the subsquent bytes left, 1 byte makes up 1 element.





ex: using hexview, the input file is:


02 00 02 00 FF 01 02 01 00 01 11 FF 22 44


then the program


should return


row = 2 (from 00 02-1st 2 bytes)


col = 2 (from (00 02 - 1st 2 bytes)


skip the next 6 bytes (FF 01 02 01 00 01)


data[row][column]= {(17,255)(34,68)} (the rest bytes)





My current program as follow


#include %26lt;stdio.h%26gt;


#include %26lt;iostream%26gt;


void infile(char i){


FILE* input = fopen(i, "rb");


int col, row, bpp;


double depth[600][600];


fclose(input);


}


int main(){


printf("Please enter input image file name");


cin %26gt;%26gt; i_f;


infile (i_f);


}|||infile should accept a char * (a C string), not a char.





Do you want to do this for a lot of bytes, until the end of the file or a sentinel? If so:





//INCOMPLETE function infile, replace some %26lt;%26gt; values.





struct Element{


unsigned int elem[10];


};





void infile(char *i)


{


FILE *f=fopen(i, "rb");


int col, row, bcount;


unsigned read;


struct Element data[255][255]={{0,0,0,0,0,0,0,0,0,0}}; //All undefined elements will have all 0s


while(!feof(f)){


fscanf("%u", %26amp;row);


if(row==%26lt;TODO: sentinel value here%26gt;)


break;


fscanf("%u", %26amp;col);


for(bcount=0; bcount%26lt;6; bcount++)


fscanf("%*u");


bcount=0;


while(read!=%26lt;TODO: seperator value (to begin new row/col line) here%26gt;){


fscanf("%u", %26amp;read);


if(read!=%26lt;TODO: seperator here%26gt;)


data[row][col].elem[bcount++]=read;}


} //End while


} //Ends infile





//End INCOMPLETE function infile





Sorry, I have to hurry, I can't test this for you.

No comments:

Post a Comment