我正在尝试读取 .bmp 图像并将数据写入文本文件。代码运行良好,但问题是,它一次无法读取整个图像,所以我必须fread()
多次调用函数。在执行此操作时,我的代码将第一次读取的数据重复存储到文本文件中。为了正确读取整个图像,我必须做哪些更改?下面是我的代码片段。
int size = width * height;
unsigned char* data = new unsigned char[size];
filename = "image.bmp";
fname = "image_data.txt";
FILE* f = fopen(filename, "rb");
FILE *fp = fopen(fname, "w");
while(totalBytes < size)
{
readsize = fread(data, sizeof(unsigned char), size, f);
totalBytes += readsize;
for(i = 0; i < readsize; i++)
{
fprintf(fp, "%d", data[i]);
if((i % width) == 0 && i != 0)
fprintf(fp, "\n");
}
fseek(f, readsize, SEEK_SET);
readsize = 0;
}