[英]Storing an image file into a buffer (gif,jpeg etc).
I'm trying to load an image file into a buffer in order to send it through a scket. 我正在尝试将图像文件加载到缓冲区中,以便通过插槽发送它。 The problem that I'm having is that the program creates a buffer with a valid size but it does not copy the whole file into the buffer.
我遇到的问题是程序创建了一个具有有效大小的缓冲区,但它没有将整个文件复制到缓冲区中。 My code is as follow
我的代码如下
//imgload.cpp
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int main(int argc,char *argv){
FILE *f = NULL;
char filename[80];
char *buffer = NULL;
long file_bytes = 0;
char c = '\0';
int i = 0;
printf("-Enter a file to open:");
gets(filename);
f = fopen(filename,"rb");
if (f == NULL){
printf("\nError opening file.\n");
}else{
fseek(f,0,SEEK_END);
file_bytes = ftell(f);
fseek(f,0,SEEK_SET);
buffer = new char[file_bytes+10];
}
if (buffer != NULL){
printf("-%d + 10 bytes allocated\n",file_bytes);
}else{
printf("-Could not allocate memory\n");
// Call exit?.
}
while (c != EOF){
c = fgetc(f);
buffer[i] = c;
i++;
}
c = '\0';
buffer[i-1] = '\0'; // helps remove randome characters in buffer when copying is finished..
i = 0;
printf("buffer size is now: %d\n",strlen(buffer));
//release buffer to os and cleanup....
return 0;
}
> output >输出
c:\Users\Desktop>imgload
-Enter a file to open:img.gif
-3491 + 10 bytes allocated
buffer size is now: 9
c:\Users\Desktop>imgload
-Enter a file to open:img2.gif
-1261 + 10 bytes allocated
buffer size is now: 7
From the output I can see that it's allocating the correct size for each image 3491 and 1261 bytes (i doubled checked the file sizes through windows and the sizes being allocated are correct) but the buffer sizes after supposedly copying is 9 and 7 bytes long. 从输出中,我可以看到它正在为每个图像分配3491和1261字节的正确大小(我通过Windows仔细检查了文件大小,并且分配的大小是正确的),但是假定复制后的缓冲区大小为9和7字节长。 Why is it not copying the entire data?.
为什么不复制整个数据?
You are wrong. 你错了。 Image is binary data, nor string data.
图像是二进制数据,也不是字符串数据。 So there are two errors:
所以有两个错误:
1) You can't check end of file with EOF
constant. 1)您不能使用
EOF
常量检查文件结尾。 Because EOF
is often defined as 0xFF and it is valid byte in binary file. 因为
EOF
通常被定义为0xFF,并且它是二进制文件中的有效字节。 So use feof()
function to check for end of file. 因此,使用
feof()
函数检查文件结尾。 Or also you may check current position in file with maximal possible (you got it before with ftell()
). 或者,您也可以最大可能地检查文件中的当前位置(之前通过
ftell()
获得了它)。
2) As file is binary it may contain \\0
in middle. 2)由于文件是二进制文件,因此中间可能包含
\\0
。 So you can't use string function to work with such data. 因此,您不能使用字符串函数来处理此类数据。
Also I see that you use C++ language. 我也看到您使用C ++语言。 Tell me please why you use classical C syntax for file working?
请告诉我为什么您使用经典的C语法进行文件处理? I think that using C++ features such as file streams, containers and iterators will simplify your program.
我认为使用C ++功能(例如文件流,容器和迭代器)将简化您的程序。
PS And I want to say that you program will have problems with really big files. PS而且我想说的是,您的程序在处理大型文件时会遇到问题。 Who knows maybe you will try to work with them.
谁知道也许您会尝试与他们合作。 If 'yes', rewrite
ftell
/ fseek
functions to their int64
( long long int
) equivalents. 如果为“是”,则将
ftell
/ fseek
函数重写为其int64
( long long int
)等效项。 Also you'll need to fix array counter. 另外,您还需要修复数组计数器。 Another good idea is to read file by blocks.
另一个好主意是按块读取文件。 Reading byte by byte is dramatically slower.
逐字节读取速度大大降低。
All this is unneeded and actually makes no sense: 所有这些都是不需要的,实际上是没有意义的:
c = '\0';
buffer[i-1] = '\0';
i = 0;
printf("buffer size is now: %d\n",strlen(buffer));
Don't use strlen
for binary data . 不要对二进制数据使用
strlen
。 strlen
stops at the first NUL
( \\0
) byte. strlen
在第一个NUL
( \\0
)字节处停止。 A binary file may contain many such bytes, so NUL
can't be used. 二进制文件可能包含许多这样的字节,因此不能使用
NUL
。
-3491 + 10 bytes allocated /* There are 3491 bytes in the file. */
buffer size is now: 9 /* The first byte with the value 0. */
In conclusion, drop that part . 总之,删除该部分 。 You already have the size of the file.
您已经有了文件的大小。
You are reading a binary file like a text file. 您正在读取二进制文件,例如文本文件。 You can't check for EOF as this could be anywhere in the binary file.
您无法检查EOF,因为它可能在二进制文件中的任何位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.