考验

zds1 2001-12-17 09:54:38
我的程序是读BMP文件,数据读出是负数为什么,我把数据改为unsigned int也不行
#include <stdio.h>
#include <malloc.h>
#include <afx.h>
struct PIXMAP
{
int rowbytes;
int height;
int size;
unsigned int * add;
};



struct IMAGE
{
int width;
int height;
int size;
char * address;
char head;
};


void main()
{

unsigned int width,height,size=NULL;
unsigned int r=NULL;
unsigned g=NULL;
unsigned int b=NULL;
unsigned int * data;
unsigned int * p=NULL;
char *y,*u,*v;
FILE * fp=NULL;
char path[20];
BITMAPFILEHEADER *head;
BITMAPINFOHEADER *info;
struct PIXMAP pixmap;
struct IMAGE plane;
printf("请输入文件路径\n");
scanf("%s",path);
if((fp=fopen(path,"rb+"))==NULL)
{
printf("空文件\n");
return;
}

else
{
head=(BITMAPFILEHEADER *)malloc(sizeof(BITMAPFILEHEADER));

info=(BITMAPINFOHEADER *)malloc(sizeof(BITMAPFILEHEADER));

fread(head,sizeof(BITMAPFILEHEADER),1,fp);
printf("%d\n",head->bfOffBits);
printf("%d\n",head->bfSize);

fread(info,sizeof(BITMAPINFOHEADER),1,fp);
width=info->biWidth;

height=info->biHeight;
printf("%d\n",info->biBitCount);
pixmap.height=height;

pixmap.size=head->bfSize-head->bfOffBits;


data=(unsigned int *)malloc(pixmap.size);
printf("%d\n",pixmap.size);

y=(char *)malloc(pixmap.size);
u=(char *)malloc(pixmap.size/4);
v=(char *)malloc(pixmap.size/4);
fread(data,pixmap.size,1,fp);
pixmap.add=data;
unsigned int g=*data;
printf("%d\n",g);
fclose(fp);


}

/* if(info->biBitCount==24)
{

pixmap.rowbytes=width*3;
for(int i=0;i<info->biHeight;i++)
{
pixmap.add+=i*pixmap.rowbytes;
for(int j=0;j<info->biWidth;j++)
{*/


p=pixmap.add;
r=*p++;
g=*p++;
b=*p++;
printf("%d,%d,%d\n",r,g,b);
// }



// }


// }
/* if(info->biBitCount==32)
{
}
else
{
}
free(data);
free(y);
free(u);
free(v);
free(head);
free(info);*/
}
...全文
42 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zds1 2001-12-17
  • 打赏
  • 举报
回复
不行,打出的数有10位
liuto 2001-12-17
  • 打赏
  • 举报
回复
你用%d打印出来当然是负数,用%u

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧