C无法读取整个BMP文件-FOPEN

时间:2019-03-28 23:24:17

标签: c fopen fread bmp

因此,我尝试读取C语言中的.bmp文件。稍后,我将使用openssl库对文件进行加密-但这只是背景信息。

我需要以二进制模式(显然)打开文件,但是由于任何原因,当我尝试打开文件时,它只能读取4个字节。当我尝试输出这个我刚打开的文件(用于错误测试)时,它输出以下内容-88 24 AD FB

在故障排除中,我决定对一个文本文件(54字节)尝试此操作,并且得到的结果完全相同。

#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(){

    char * fileName="pic_original.bmp";

    //read the file from given filename in binary mode
    printf("Start to read the .bmp file \n");

    FILE *image;
    image = fopen(fileName,"rb");

    //print the size of the image (4 bytes every damn time)
    printf("Size of image: %d\n",sizeof(image));

    //output the exact file that was read (error testing)
    FILE *test;
    test = fopen("./test.bin", "w");
    fwrite(image, sizeof(image), 1, test);

    fclose(test);
    fclose(image);


    return 1;
}

这是图片(由于某种原因被上载为png)

image

不确定我在哪里出错了,但我不太熟悉C。

干杯, 利亚姆

编辑1:

//allocate memory for the header and image
char *headerBuf = (char *)malloc(54);
char *imageBuf = (char *)malloc(sizeof(image)-54); //this line is wrong - thanks to user EOF

//allocate memory for the final ciphertext
char *imagecipherCBC = (char *)malloc(sizeof(image)); //wrong also

//read first 54 bytes (header)
rewind(image);
fread(headerBuf,54,1,image);

//read the bitmap image until the end of the file
fread(imageBuf,sizeof(image),1,image); //also wrong

2 个答案:

答案 0 :(得分:1)

如果您的目标是加密文件,则将整个文件读入缓冲区,对其进行加密,然后将其保存为二进制文件。您可以通过将文件指针移到末尾来找到文件大小。示例:

int main()
{
    FILE *fin;
    fin = fopen("pic_original.bmp", "rb");
    fseek(fin, 0, SEEK_END);
    int filesize = ftell(fin);
    rewind(fin);

    char *buf = malloc(filesize);
    fread(buf, 1, filesize, fin);
    fclose(fin);

    //encrypt the buffer...

    FILE *fout = fopen("output.bmp", "wb");
    fwrite(buf, 1, filesize, fout);
    fclose(fout);

    return 0;
}

这将适用于任何文件。 OpenSSL已经具有直接加密文件的功能。

如果出于某种原因您希望保持标题不变,并且仅更改其后的位,则分别读取标题:

int main()
{
    FILE *fin = fopen("input.bmp", "rb");
    if(!fin) { printf("cannot open input\n"); return 0; }

    FILE *fout = fopen("output.bmp", "wb");
    if(!fout) { printf("cannot open output\n"); return 0; }

    fseek(fin, 0, SEEK_END);
    int filesize = ftell(fin);
    if(filesize <= 54)
    {
        printf("wrong filesize\n");
        return 0;
    }
    rewind(fin);

    char *header = malloc(54);
    char *buf = malloc(filesize - 54);
    //encrypt buf...
    fread(header, 1, 54, fin);
    fread(buf, 1, filesize - 54, fin);
    fclose(fin);

    fwrite(header, 1, 54, fout);
    fwrite(buf, 1, filesize - 54, fout);
    fclose(fout);

    free(header);
    free(buf);
    return 0;
}

我想这具有加密的位图仍将被识别为位图的优势。但是只有加密方法不会在输出中添加额外的字节。

请注意,8位,4位和单色位图具有一个调色板,该调色板位于54字节标题之后,然后是图像位。

答案 1 :(得分:1)

好吧, 图像的大小当然是4个字节,这是32位计算机上的文件指针。

作为一个简单的示例,我认为您必须准备bmp文件的某些图像缓冲区,然后,如果文件不太大,则可以对该图像缓冲区的内容进行加密和解密。

static void read_from_image(char *imageBuf, int fileLength)
{
    const char * outFileName="c:/DEV/temp/test.bin";
    char headerBuf[54];
    char *imagecipherCBC;

    FILE *test;
    test = fopen(outFileName, "wb");

    //allocate memory for the final ciphertext
    imagecipherCBC = (char *)malloc(fileLength *sizeof(char));

    //read first 54 bytes (header)
    //fread(headerBuf,54,1,image);
    memcpy(headerBuf, imageBuf, 54 * sizeof(char));

    //read the bitmap image until the end of the file
    //fread(imageBuf,sizeof(image),1,image); //also wrong

    fwrite(imageBuf, fileLength * sizeof(char), 1, test);
    fflush(test);
    fclose(test);

    free(imagecipherCBC),imagecipherCBC = NULL;
    free(imageBuf),imageBuf = NULL;

    return;
}

您可以在主要功能中拥有文件长度和图像缓冲区。

    int main(int argc, char *argv[])
{
    const char * fileName="c:/DEV/temp/pic_original.bmp";

    int fileLength = 0;

    FILE *image;
    char *imageBuffer;

    imageBuffer = NULL;
    image = fopen(fileName,"rb");

    printf("read the file from given filename in binary mode \n");
    printf("Start to read the .bmp file \n");

    //try to get a file length;
    fseek(image, 0, SEEK_END);
    fileLength = ftell(image);
    fseek(image, 0, SEEK_SET);
    rewind(image);

    imageBuffer = (char*)malloc(fileLength * sizeof(char));

    //print the size of the image (4 bytes every damn time)
    printf("read the file from given filename in binary mode \n");
    printf("Size of image file pointer: %d\n",sizeof(image));
    printf("Size of image: %d\n",fileLength);

    //output the exact file that was read (error testing)
    fread(imageBuffer,sizeof(char),fileLength*sizeof(char), image);

    fclose(image);

    read_from_image(imageBuffer, fileLength);

    return 0;
}

祝你好运