文件的读取问题

jinjiajie 2006-03-01 10:50:32
我写了一个读取bmp图片的程序,但是24位的图像都会在读几个字节后就read不出来了,read函数的返回值为-1,为什么会出现这种情况的?有什么办法能解决吗?
...全文
104 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jinjiajie 2006-03-01
  • 打赏
  • 举报
回复
....调了2天了,就是有几张图会莫名其妙的读不出来了,能给点可能的原因我好找
yajyjy 2006-03-01
  • 打赏
  • 举报
回复
注意文件指针
多做调试,跟踪读文件过程
jinjiajie 2006-03-01
  • 打赏
  • 举报
回复
int showbmp(HWND hWnd)
{
HDC hTuxiang;
int fp;
int size;
int size1;
char *headerbuffer;
char *rgbgurdbuffer;
int DataSizePerLine;
unsigned char byte;
unsigned char byte1;
unsigned char byte2;
RGBQUAD1 rgbb;
int a;
BITMAPFILEHEADER bmfheader;
BITMAPINFOHEADER bmiheader;
hTuxiang=GetDC(hWnd);
if((fp=open("Winter.bmp",O_RDONLY&O_TEXT)) == -1)
{
exit(0);
}

size=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);

headerbuffer=(char *)malloc(size);
read(fp,headerbuffer,size);
//文件头读取
bmfheader=*(BITMAPFILEHEADER *)headerbuffer;
bmiheader=*(BITMAPINFOHEADER *)(headerbuffer+sizeof(BITMAPFILEHEADER));
DataSizePerLine=((bmiheader.biWidth)* (bmiheader.biBitCount)+31)/8/4*4;
//数据部分处理
switch(bmiheader.biBitCount)
{
case 1:
RGBQUAD rgb2[2];
unsigned char byte21;
unsigned char byte22;
unsigned char byte23;
unsigned char byte24;
unsigned char byte25;
unsigned char byte26;
unsigned char byte27;
unsigned char byte28;
size1=2*sizeof(RGBQUAD);
rgbgurdbuffer=(char *)malloc(size1);
read(fp,rgbgurdbuffer,size1);
for(int i=0;i<2;i++)
{
rgb2[i]=*(RGBQUAD *)(rgbgurdbuffer+(i)*sizeof(RGBQUAD));
}
lseek(fp,bmfheader.bfOffBits,SEEK_SET);
for(int y=0;y<bmiheader.biHeight;y++)
{
for(i=0;i<DataSizePerLine;i++)
{
if((a=read(fp,&byte,1))>0)
{
byte21=(byte>>7)&0x1;
byte22=(byte>>6)&0x1;
byte23=(byte>>5)&0x1;
byte24=(byte>>4)&0x1;
byte25=(byte>>3)&0x1;
byte26=(byte>>2)&0x1;
byte27=(byte>>1)&0x1;
byte28=(byte)&0x1;
SetPixel(hTuxiang,100+8*i,600-y, RGB(rgb2[byte21].rgbRed,rgb2[byte21].rgbGreen, rgb2[byte21].rgbBlue));
SetPixel(hTuxiang,100+8*i+1,600-y, RGB(rgb2[byte22].rgbRed,rgb2[byte22].rgbGreen, rgb2[byte22].rgbBlue));
SetPixel(hTuxiang,100+8*i+2,600-y, RGB(rgb2[byte23].rgbRed,rgb2[byte23].rgbGreen, rgb2[byte23].rgbBlue));
SetPixel(hTuxiang,100+8*i+3,600-y, RGB(rgb2[byte24].rgbRed,rgb2[byte24].rgbGreen, rgb2[byte24].rgbBlue));
SetPixel(hTuxiang,100+8*i+4,600-y, RGB(rgb2[byte25].rgbRed,rgb2[byte25].rgbGreen, rgb2[byte25].rgbBlue));
SetPixel(hTuxiang,100+8*i+5,600-y, RGB(rgb2[byte26].rgbRed,rgb2[byte26].rgbGreen, rgb2[byte26].rgbBlue));
SetPixel(hTuxiang,100+8*i+6,600-y, RGB(rgb2[byte27].rgbRed,rgb2[byte27].rgbGreen, rgb2[byte27].rgbBlue));
SetPixel(hTuxiang,100+8*i+7,600-y, RGB(rgb2[byte28].rgbRed,rgb2[byte28].rgbGreen, rgb2[byte28].rgbBlue));
}else if(a==0)
{
}else{
exit(0);
}
}
}
break;
case 4:
RGBQUAD rgb16[16];
size1=16*sizeof(RGBQUAD);
rgbgurdbuffer=(char *)malloc(size1);
read(fp,rgbgurdbuffer,size1);
for(int i=0;i<16;i++)
{
rgb16[i]=*(RGBQUAD *)(rgbgurdbuffer+(i)*sizeof(RGBQUAD));
}
lseek(fp,bmfheader.bfOffBits,SEEK_SET);
for(int y=0;y<bmiheader.biHeight;y++)
{
for(i=0;i<DataSizePerLine;i++)
{
if((a=read(fp,&byte,1))>0)
{
byte1=byte&0xf;
byte2=(byte>>4)&0xf;
SetPixel(hTuxiang,100+2*i,600-y, RGB(rgb16[byte1].rgbRed,rgb16[byte1].rgbGreen, rgb16[byte1].rgbBlue));
SetPixel(hTuxiang,100+2*i+1,600-y, RGB(rgb16[byte2].rgbRed,rgb16[byte2].rgbGreen, rgb16[byte2].rgbBlue));
}else if(a==0)
{
}else{
exit(0);
}
}
}
break;
case 8:
RGBQUAD rgb256[256];
size1=256*sizeof(RGBQUAD);
rgbgurdbuffer=(char *)malloc(size1);
read(fp,rgbgurdbuffer,size1);
for(int i=0;i<256;i++)
{
rgb256[i]=*(RGBQUAD *)(rgbgurdbuffer+(i)*sizeof(RGBQUAD));
}
lseek(fp,bmfheader.bfOffBits,SEEK_SET);
for(int y=0;y<bmiheader.biHeight;y++)
{
for(i=0;i<DataSizePerLine;i++)
{
if(a=read(fp,&byte,1)>0)
{
SetPixel(hTuxiang,100+i,600-y, RGB(rgb256[byte].rgbRed,rgb256[byte].rgbGreen, rgb256[byte].rgbBlue));
}else if(a==0)
{

}else{
exit(0);
}
}
}
break;
case 24:
//lseek(fp,bmfheader.bfOffBits,SEEK_SET);
for(int y=0;y<bmiheader.biHeight;y++)
{
for(i=0;i<DataSizePerLine;i=i+3)
{
if(a=read(fp,&rgbb,sizeof(RGBQUAD1))>0)
/*read(fp,&byte1,sizeof(char));
read(fp,&byte2,sizeof(char));*/
SetPixel(hTuxiang,100+i/3,600-y, RGB(rgbb.rgbRED,rgbb.rgbGREEN,rgbb.rgbBLUE));
else if(a==0){
}else{
lseek(fp,3,SEEK_END);
}
}
}

break;
default :
exit(0);
}
//rgb=*(RGBQUAD *)(headerbuffer+sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER));
//bmiheader.biBitCount;
if(bmiheader.biBitCount==1 ||bmiheader.biBitCount==4 ||bmiheader.biBitCount==8)
free(rgbgurdbuffer);
free(headerbuffer);
close(fp);
if(ReleaseDC(hWnd,hTuxiang )==0)
{
exit(0);
}
return 1;
}
现在的情况是有几张2位的图读一半就认为结束了,24位的图转为16位后然后转成24位的能显示,不转过就显示100多个像素后救停了...纳闷中
逸学堂 2006-03-01
  • 打赏
  • 举报
回复
用文本方式读取。
最好贴出代码
屋顶上的老猫 2006-03-01
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/4386/4386898.xml?temp=.4813959
jinjiajie 2006-03-01
  • 打赏
  • 举报
回复
o ,搞错了,是一个文件明明很大,但是却很快就说读尾了....这个是怎么回事情啊?有人能帮我分析下吗?
jinjiajie 2006-03-01
  • 打赏
  • 举报
回复
哎...头大了,没人和我一样遇到类似情况的吗?

69,371

社区成员

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

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