fread() 中的段错误

发布于 2024-10-25 08:33:47 字数 1407 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情深已缘浅 2024-11-01 08:33:48

摆脱 malloc 调用中的强制转换。和#include

在您使用的 64 位和 32 位系统中,该程序的输出是什么?

#include <stdio.h>
int main(void) {
    printf("sizeof (int) is %d\n", (int)(sizeof (int)));
    printf("sizeof (int*) is %d\n", (int)(sizeof (int*)));
    return 0;
}

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?

#include <stdio.h>
int main(void) {
    printf("sizeof (int) is %d\n", (int)(sizeof (int)));
    printf("sizeof (int*) is %d\n", (int)(sizeof (int*)));
    return 0;
}
倾城花音 2024-11-01 08:33:48
  1. 在调试器中运行您的程序。

  2. 在调用处设置断点
    fread -- 使其有条件
    k==507(当你
    期望 fread 成功)。

  3. 当程序点击
    断点,检查变量
    并检查即将通过的内容
    害怕。第一次或两次
    你到达断点,值
    会很好的。

  4. 然后在第509次,你会
    可能会看到传递的虚假值
    害怕。找出那些在哪里
    虚假的价值观来自——
    可能会设置一个条件
    正在设置的变量上的断点
    无论虚假值是什么。

  1. Run your program in the debugger.

  2. 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).

  3. 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.

  4. 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.

一身骄傲 2024-11-01 08:33:47

将其设为 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.

心清如水 2024-11-01 08:33:47

您从哪里获得101376?每个字节最多占用 5 个字符,作为带逗号的十进制数(例如 -127,),5*26368131840

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 is 131840.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文