Search code examples
cbmp

Bad ptr on bmp read c


I get a error on the following code:

FILE    *fp;
int     patno;
int     h, i, j, l, m;
int     i1, i2, i3;


 if( (fp=fopen(filename, "r")) == NULL ) {
    printf("\"%s\" not found!!\n", filename);
    return(-1);
}

for( h=0; h<4; h++ ) {
    l = 0;
    for( i3 = 0; i3 < 3; i3++ ) {
        for( i2 = 0; i2 < PATTERN_HEIGHT; i2++ ) {
            for( i1 = 0; i1 < PATTERN_WIDTH; i1++ ) {
                if( fscanf(fp, "%d", &j) != 1 ) {
                    printf("Pattern Data read error!!\n");
                    return -1;
                }
                 --..rest of code..--

the file exists and the path is right, the line if(fscanf(fp...) the pointer fp has the value of 0x00000000<Bad ptr>..

Any ideas?


Solution

  • fscanf is returning 0 instead of 1 because bmp files don't contain numbers in textual form. You need to open the file in binary (mode "rb") and extract fields with fread.