社区
C语言
帖子详情
文件的读取问题
jinjiajie
2006-03-01 10:50:32
我写了一个读取bmp图片的程序,但是24位的图像都会在读几个字节后就read不出来了,read函数的返回值为-1,为什么会出现这种情况的?有什么办法能解决吗?
...全文
104
7
打赏
收藏
文件的读取问题
我写了一个读取bmp图片的程序,但是24位的图像都会在读几个字节后就read不出来了,read函数的返回值为-1,为什么会出现这种情况的?有什么办法能解决吗?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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
打赏
举报
回复
哎...头大了,没人和我一样遇到类似情况的吗?
python-
文件
操作
本课程主要讲解python中针对
文件
的操作,主要内容如下: 1.
文件
作用 2.
文件
访问模式 3.
文件
打开 4.
文件
关闭 5.
文件
读取
6.
文件
写入 7.
文件
备份 8. 批量修改
文件
SpringBoot多模块下yml
文件
读取
问题
如果在多个模块中都有application.yml
文件
,则会只
读取
有主启动类中的yml
文件
,不会
读取
其他模块中的配置
文件
,例如:在controller模块中有一个application.yml
文件
,在dao模块中有一个application.yml,如果启动...
【matlab】对每一行数据不等的txt
文件
读取
问题
文件
读取
方法 目标:
读取
每一行数字数目不同的纯文本
文件
。形如: 9 2 8 9 7 3 4 6 8 9 0 0 2 8 7 4 0 1 6 0 0 8 0 2 7 4 4 6 2 8 0 2 7 8 0 2 6 8 0 2 0 5 0
python
文件
的
读取
1.
文件
的
读取
1.read()
读取
整个
文件
2.readline() 每次
读取
一行
文件
3. readlines()
读取
文件
的所有行 2.
文件
的写入 1.以"x"方式打开
文件
2.以"a"方式打开
文件
3.以"w"方式打开
文件
3.
文件
的删除 4.Excel表数据的...
2021-07-09 CTFer成长之路-任意
文件
读取
漏洞-
文件
读取
漏洞常见
读取
路径(liunx)
一:
文件
读取
漏洞常见触发点 1.Web语言 不同的Web语言,其
文件
读取
漏洞的触发点也会存在差异,本小节以
读取
不同Web
文件
漏洞为例进行介绍,具体的漏洞场景请读者自行查阅,在此不再赘述。 1.1 PHP PHP标准函数中有关...
C语言
69,371
社区成员
243,080
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章