C-在project 2017.1.exe中抛出的BMP文件远程验证的错误(ucrtbased.dll)在0x0FDDD



在此代码中,我尝试在每个像素中获取第一个字节,从一个BMP文件中,每个像素的RGB值相同(例如FF FF FF FF(,然后放置这些值进入矩阵以进行进一步的操作。调试过程被终止时终止调试器"从每个像素中获取第一个字节"one_answers" 2017.1.exe:0xc0000005:访问违规写作位置0xCDCDCDCDCD"的" 2017.1.exe:0xcdc0000005"中的0x0fddd053f(ucrtbaseed.dll(抛出的"异常"。有什么问题?(这是我第一次与BMP合作(感谢您的帮助!

#include <stdio.h>
#include <stdlib.h>
main() {
    int width, height, w_count, h_count, padding;
    int **pix_mat;
    int a;
    FILE * dust_pic;
    fopen_s(&dust_pic, "d2.bmp", "rb");
    if (!dust_pic)
        printf_s("not found");
    fseek(dust_pic, 18, SEEK_SET);
    fread(&width, sizeof(int), 1, dust_pic);// getting width
    fread(&height, sizeof(int), 1, dust_pic);// getting height
    // FIND PADDING
    a = width;
    if (a % 4 != 0)
        padding = 4 - (a % 4);
    else
    padding = 0;
    //making matrix for pixels
    pix_mat = (int**)malloc(sizeof(int*)*height);
    for (h_count = 0; h_count < height; h_count++)
        pix_mat[h_count] = (int*)malloc(sizeof(int)*width);
    fseek(dust_pic, 54, SEEK_SET);
    // getting the first byte from each pixel
    for (h_count = 0; h_count < height; h_count++)
    {
        for (w_count = 0; w_count < width; w_count++)
        {
            fread(pix_mat[w_count][h_count], sizeof(char), 1, dust_pic);
            fseek(dust_pic, 2, SEEK_CUR);
        }
        fseek(dust_pic, padding, SEEK_CUR);
    }
    fclose(dust_pic);
    // print the matrix.
    for (h_count = 0; h_count < height; h_count++)
    {
        for (w_count = 0; w_count < width; w_count++)
            printf_s("[%d],[%d]=%d  ", w_count, h_count, pix_mat[w_count][h_count]);
        printf("n");
    }
    // free array
    for (h_count = 0; h_count < height; h_count++)
        free(pix_mat[h_count]);
    free(pix_mat);
}

以下提出的代码:

  1. 清洁编译
  2. 正确检查错误
  3. 在任何错误事件后退出之前,正确清理
  4. 正确读取文件中的数据大小,并将数据保存到正确声明的数组中
  5. 正确引用内存中像素矩阵的行和列
  6. 没有纠正有关像素宽度的假设,偏移到文件中的像素等等,等等。

现在代码

#include <stdio.h>  // fopen(), fclose(), perror(), fseek(), SEEK_SET
#include <stdlib.h> // exit(), EXIT_FAILURE, malloc(), calloc(), free()
// prototypes
void cleanup( FILE *dust_pic, size_t height, char **pix_mat );

int main( void )
{
    size_t width, height, w_count, h_count;
    long padding;
    char **pix_mat;
    FILE * dust_pic;
    dust_pic = fopen("d2.bmp", "rb");
    if (!dust_pic)
    {
        perror( "fopen failed" );
        exit( EXIT_FAILURE );
    }
    // implied else, fopen_s successful
    fseek(dust_pic, 18, SEEK_SET);
    // check for error
    if( 1 != fread(&width, sizeof(int), 1, dust_pic) )// getting width
    {
        perror( "fread for width of image, in pixels, failed" );
        fclose( dust_pic );
        exit( EXIT_FAILURE );
    }
    // implied else, fread successful
    if( 1 != fread(&height, sizeof(int), 1, dust_pic) )// getting height
    {
        perror( "fread for height of image, in pixels, failed" );
        fclose( dust_pic );
        exit( EXIT_FAILURE );
    }
    // implied else, fread successful
    // calculate PADDING
    if ( width % 4 != 0)
        padding = (long)(4 - (width % 4));
    else
        padding = 0;
    //making matrix for pixels
    pix_mat = calloc(height, sizeof(char*));
    // check for malloc status
    if( !pix_mat )
    {
        perror( "calloc for array of pointers to char failed" );
        fclose( dust_pic );
        exit( EXIT_FAILURE );
    }
    // implied else, calloc successful
    for (h_count = 0; h_count < height; h_count++)
    {
        pix_mat[h_count] = malloc(sizeof(char)*width);
        // check for malloc status
        if( !pix_mat[h_count] )
        {
            perror( "malloc failed" );
            fclose( dust_pic );
            exit( EXIT_FAILURE );
        }
        // implied else, malloc successful
    }
    // jump to beginning of pixel data in file
    if( -1 == fseek(dust_pic, 54, SEEK_SET) )
    {
        perror( "fseek to start of pixel data failed" );
        cleanup( dust_pic, height, pix_mat );
        exit( EXIT_FAILURE );
    }
    // implied else, fseek successful
    // get the first byte (red) from each pixel
    for (h_count = 0; h_count < height; h_count++)
    {
        for (w_count = 0; w_count < width; w_count++)
        {
            if( 1 != fread( &((pix_mat[h_count])[w_count]), sizeof(char), 1, dust_pic) )
            {
                perror( "fread of 'red' pixel value failed" );
                cleanup( dust_pic, height, pix_mat );
                exit( EXIT_FAILURE );
            }
            // implied else, fread successful
            if( -1 == fseek(dust_pic, 2, SEEK_CUR) )
            {
                perror( "fseek to skip green/blue pixels failed" );
                cleanup( dust_pic, height, pix_mat );
                exit( EXIT_FAILURE );
            }
            // implied else, fseek successful
        }
        if( -1 == fseek(dust_pic, padding, SEEK_CUR) )
        {
            perror( "fseek to skip padding failed" );
            cleanup( dust_pic, height, pix_mat );
            exit( EXIT_FAILURE );
        }
        // implied else, fseek successful
    }

    // print the matrix.
    for (h_count = 0; h_count < height; h_count++)
    {
        for (w_count = 0; w_count < width; w_count++)
            printf("[%lu],[%lu]=%3.3d  ", h_count, w_count, (pix_mat[h_count])[w_count]);
        printf("n");
    }
    cleanup( dust_pic, height, pix_mat );
} // end function: main

void cleanup( FILE *dust_pic, size_t height, char **pix_mat )
{
    // free array
    for (size_t h_count = 0; h_count < height; h_count++)
        free(pix_mat[h_count]);
    free(pix_mat);
    fclose( dust_pic );
} // end function: cleanup

最新更新