fread() 中的段错误
我正在尝试使用 C 读取 BMP 图像(灰度),将值保存到数组中,并将该数组转换为值以逗号分隔的字符串。
我的程序在 Windows 7 64 位下运行良好,但由于库兼容性问题,我不得不迁移到 Windows XP 32 位。
我有 1,750 张图像要读取,我想将它们全部存储在一个字符串中。
当我启动程序时,直到第 509 个图像为止一切正常,然后我收到由 fread() 引起的分段错误。这是我的代码:
int i=0,j,k,num,len,length,l;
unsigned char *Buffer;
FILE *fp;
char *string,*finalstring;
char *query;
char tmp2[5],tmp[3];
query = (char *)malloc(sizeof(char)*200000000);
string = (char *)malloc(sizeof(char)*101376);
Buffer = (unsigned char *)malloc(sizeof(unsigned char)*26368);
BITMAPFILEHEADER bMapFileHeader;
BITMAPINFOHEADER bMapInfoHeader;
length = 0;
for (k =1;k<1751;k++)
{
strcpy(link,"imagepath");
//here just indexing the images from 0000 to 1750
sprintf(tmp2,"%.4d",k);
strcat(link,tmp2);
strcat(link,".bmp");
fp = fopen(link, "rb");
num = fread(&bMapFileHeader,sizeof(BITMAPFILEHEADER),1,fp);
num = fread(&bMapInfoHeader,sizeof(BITMAPINFOHEADER),1,fp);
//seek beginning of data in bitmap
fseek(fp,54,SEEK_SET);
//read in bitmap file to data
fread(Buffer,26368,1,fp);
l=0;
for(i=1024;i<26368;i++)
{
itoa(Buffer[i],tmp,10);
len = strlen(tmp);
memcpy(string+l,tmp,len);
memcpy(string+l+len,",",1);
l = l+len+1;
}
memcpy(query,"",1);
memcpy(string,"",1);
printf("%i\n",k);
}
谢谢
I'm trying to read a BMP image (greyscales) with C, save values into an array, and convert this array to a string with values separated with a comma.
My program worked well under Windows 7 64-bit, but I had to move to Windows XP 32-bit because of library compatibility problems.
I have 1,750 images to read, and I want to store all of them in a single string.
When I launch my program it goes fine until the 509:th image, then I get a Segmentation Fault caused by fread(). Here's my code:
int i=0,j,k,num,len,length,l;
unsigned char *Buffer;
FILE *fp;
char *string,*finalstring;
char *query;
char tmp2[5],tmp[3];
query = (char *)malloc(sizeof(char)*200000000);
string = (char *)malloc(sizeof(char)*101376);
Buffer = (unsigned char *)malloc(sizeof(unsigned char)*26368);
BITMAPFILEHEADER bMapFileHeader;
BITMAPINFOHEADER bMapInfoHeader;
length = 0;
for (k =1;k<1751;k++)
{
strcpy(link,"imagepath");
//here just indexing the images from 0000 to 1750
sprintf(tmp2,"%.4d",k);
strcat(link,tmp2);
strcat(link,".bmp");
fp = fopen(link, "rb");
num = fread(&bMapFileHeader,sizeof(BITMAPFILEHEADER),1,fp);
num = fread(&bMapInfoHeader,sizeof(BITMAPINFOHEADER),1,fp);
//seek beginning of data in bitmap
fseek(fp,54,SEEK_SET);
//read in bitmap file to data
fread(Buffer,26368,1,fp);
l=0;
for(i=1024;i<26368;i++)
{
itoa(Buffer[i],tmp,10);
len = strlen(tmp);
memcpy(string+l,tmp,len);
memcpy(string+l+len,",",1);
l = l+len+1;
}
memcpy(query,"",1);
memcpy(string,"",1);
printf("%i\n",k);
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
摆脱。
malloc
调用中的强制转换。和#include在您使用的 64 位和 32 位系统中,该程序的输出是什么?
Get rid of the casts in
malloc
calls. And#include <stdlib.h>
.What's the output of this program, in both the 64-bit and 32-bit systems you're using?
在调试器中运行您的程序。
在调用处设置断点
fread -- 使其有条件
k==507(当你
期望 fread 成功)。
当程序点击
断点,检查变量
并检查即将通过的内容
害怕。第一次或两次
你到达断点,值
会很好的。
然后在第509次,你会
可能会看到传递的虚假值
害怕。找出那些在哪里
虚假的价值观来自——
可能会设置一个条件
正在设置的变量上的断点
无论虚假值是什么。
Run your program in the debugger.
Set a breakpoint at the call to
fread -- make it conditional on
k==507 (this will stop it when you
expect the fread to be successful).
When the program hits the
breakpoint, examine the variables
and check what is about to be passed
to fread. The first one or two times
you hit the breakpoint, the values
will be good.
Then on the 509th time, you will
probably see bogus values being passed
to fread. Figure out where those
bogus values are coming from --
possibly set a conditional
breakpoint on the variable being set
to whatever the bogus value is.
将其设为
tmp[4];
表示三位数字并以 0 结尾。另外:
fclose
在哪里?我怀疑你的文件句柄用完了。检查是否
fp != 0
。Make it
tmp[4];
for three digits and the terminating 0.Also: where is the
fclose
? I suspect that you're running out of file handles.Check, whether
fp != 0
.您从哪里获得
101376
?每个字节最多占用 5 个字符,作为带逗号的十进制数(例如-127,
),5*26368
为131840
。Where did you get
101376
from? Each of your bytes take up at most 5 characters as a decimal number with comma (e.g.-127,
),5*26368
is131840
.