sil*_*ent 0 c++ file-io bufferedimage image
我正在尝试将图像文件加载到缓冲区中以便通过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;
}
Run Code Online (Sandbox Code Playgroud)
>输出
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
Run Code Online (Sandbox Code Playgroud)
从输出我可以看到它为每个图像3491和1261字节分配正确的大小(我通过窗口检查文件大小加倍并且分配的大小是正确的)但是假设复制后的缓冲区大小是9和7字节长.为什么不复制整个数据?
你错了.图像是二进制数据,也不是字符串数据.所以有两个错误:
1)您无法使用EOF常量检查文件结尾.因为EOF通常定义为0xFF并且它是二进制文件中的有效字节.所以使用feof()函数来检查文件的结尾.或者你也可以用最大可能的方式检查文件中的当前位置(你之前得到它ftell()).
2)由于文件是二进制文件,它可能包含\0在中间.所以你不能使用字符串函数来处理这些数据.
我也看到你使用的是C++语言.请告诉我为什么你使用经典的C语法来处理文件?我认为使用C++功能,如文件流,容器和迭代器将简化您的程序.
PS我想说你的程序会有很大的文件问题.谁知道也许你会尝试与他们合作.如果为"是",则将ftell/ (或)fseek函数重写为其int64(long long int)等效项.你还需要修复阵列计数器.另一个好主意是按块读取文件.逐字节读取速度要慢得多.