将图像文件存储到缓冲区(gif、jpeg 等)。
我正在尝试将图像文件加载到缓冲区中,以便通过 scket 发送它。我遇到的问题是程序创建了一个具有有效大小的缓冲区,但它不会将整个文件复制到缓冲区中。我的代码如下
//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;
}
>输出
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
从输出中我可以看到它为每个图像分配了正确的大小 3491 和 1261 字节(我通过 Windows 仔细检查了文件大小,分配的大小是正确的),但据称复制后的缓冲区大小为 9和 7 个字节长。为什么它不复制整个数据?
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. Why is it not copying the entire data?.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您错了。图像是二进制数据,也不是字符串数据。所以有两个错误:
1)你不能用
EOF
常量检查文件结尾。因为EOF
通常被定义为0xFF,它是二进制文件中的有效字节。因此,使用feof()
函数来检查文件结尾。或者您也可以尽可能检查文件中的当前位置(您之前使用ftell()
获得过它)。2) 由于文件是二进制文件,因此中间可能包含
\0
。因此,您不能使用字符串函数来处理此类数据。我还看到你使用C++语言。请告诉我为什么你使用经典的 C 语法来处理文件?我认为使用文件流、容器和迭代器等 C++ 功能将简化您的程序。
PS 我想说的是,你的程序在处理非常大的文件时会出现问题。谁知道也许您会尝试与他们合作。如果“是”,请将
ftell
/fseek
函数重写为其等效的int64
(long long int
) 函数。您还需要修复数组计数器。另一个好主意是按块读取文件。逐字节读取速度要慢得多。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. BecauseEOF
is often defined as 0xFF and it is valid byte in binary file. So usefeof()
function to check for end of file. Or also you may check current position in file with maximal possible (you got it before withftell()
).2) As file is binary it may contain
\0
in middle. So you can't use string function to work with such data.Also I see that you use C++ language. Tell me please why you use classical C syntax for file working? I think that using C++ features such as file streams, containers and iterators will simplify your program.
P.S. And I want to say that you program will have problems with really big files. Who knows maybe you will try to work with them. If 'yes', rewrite
ftell
/fseek
functions to theirint64
(long long int
) equivalents. Also you'll need to fix array counter. Another good idea is to read file by blocks. Reading byte by byte is dramatically slower.所有这些都是不必要的,而且实际上没有任何意义:
不要将
strlen
用于二进制数据。strlen
在第一个NUL
(\0
) 字节处停止。二进制文件可能包含许多这样的字节,因此不能使用NUL
。总之,删除该部分。您已经知道了文件的大小。
All this is unneeded and actually makes no sense:
Don't use
strlen
for binary data.strlen
stops at the firstNUL
(\0
) byte. A binary file may contain many such bytes, soNUL
can't be used.In conclusion, drop that part. You already have the size of the file.
您正在像读取文本文件一样读取二进制文件。您无法检查 EOF,因为它可能位于二进制文件中的任何位置。
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.