C未读取整个BMP文件-fopen
因此,我尝试读取C中的.bmp文件。稍后我将使用openssl库对该文件进行加密,但这只是背景信息 我需要以二进制模式打开文件(显然),但无论出于什么原因,当我试图打开文件时,它只读取4个字节。当我试图输出我刚刚打开的这个文件(用于错误测试)时,它会输出以下内容-C未读取整个BMP文件-fopen,c,fopen,fread,bmp,C,Fopen,Fread,Bmp,因此,我尝试读取C中的.bmp文件。稍后我将使用openssl库对该文件进行加密,但这只是背景信息 我需要以二进制模式打开文件(显然),但无论出于什么原因,当我试图打开文件时,它只读取4个字节。当我试图输出我刚刚打开的这个文件(用于错误测试)时,它会输出以下内容-88 24 AD FB 在我的故障排除中,我决定在一个文本文件(54字节)上尝试这个方法,得到了完全相同的结果 #include <openssl/conf.h> #include <openssl/evp.h>
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;
}
如果您的目标是加密文件,则将整个文件读入缓冲区,对其进行加密,然后将其另存为二进制文件。通过将文件指针移到末尾,可以找到文件大小。例如:
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;
}
intmain()
{
FILE*fin=fopen(“input.bmp”、“rb”);
如果(!fin){printf(“无法打开输入”\n”);返回0;}
FILE*fout=fopen(“output.bmp”、“wb”);
如果(!fout){printf(“无法打开输出\n”);返回0;}
fseek(fin,0,SEEK_END);
int filesize=ftell(fin);
if(filesize如果您的目标是加密文件,则将整个文件读入缓冲区,对其进行加密,并将其另存为二进制文件。您可以通过将文件指针移到末尾来查找文件大小。示例:
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;
}
intmain()
{
FILE*fin=fopen(“input.bmp”、“rb”);
如果(!fin){printf(“无法打开输入”\n”);返回0;}
FILE*fout=fopen(“output.bmp”、“wb”);
如果(!fout){printf(“无法打开输出\n”);返回0;}
fseek(fin,0,SEEK_END);
int filesize=ftell(fin);
如果(文件大小良好,
图像的大小当然是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;
}
祝你好运,
图像的大小当然是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;
}
祝你好运sizeof(图像)
是sizeof(文件*)
和sizeof(文件*)
不取决于文件的大小。嗯,好吧,那么我如何输出我读入的确切文件?我是否正确读入了文件?这取决于。你在哪个平台上?你必须只使用portable c,还是可以使用该平台?如果你在POSIX上,fstatat()
是一个合理的选项,前提是您正在读取文件而不是常规(不可查找)流。使用位图文件,首先可以将头读入结构中,该结构将告诉您图像大小。然后可以为位图分配内存并读取。或者,如果您只是复制文件:大小\u t字节;而((字节=fread(buffer,1,sizeof buffer,image))!=0{fwrite(buffer,1,bytes,test);}
sizeof(image)
是sizeof(FILE*)
和sizeof(FILE*)
不取决于文件的大小。嗯,好吧,那么我如何输出我读入的确切文件?我是否正确读入了文件?这取决于。你在哪个平台上?你必须只使用portable c,还是可以使用该平台?如果你在POSIX上,fstatat()
是一个合理的选项,前提是您正在读取文件而不是常规(不可查找)流。使用位图文件,首先可以将头读入结构中,该结构将告诉您图像大小。然后可以为位图分配内存并读取。或者,如果您只是复制文件:大小\u t字节;而((字节=fread(buffer,1,sizeof buffer,image))!=0{fwrite(buffer,1,bytes,test);}
我花了一段时间才理解它,因为我不懂C,但这个解释很完美!谢谢伙计!我花了一段时间才理解它,因为我不懂C,但这个解释很完美!谢谢伙计!这个代码适用于基本的单色bmp文件吗?这个代码适用于基本的单色bmp文件吗?