社区
C语言
帖子详情
文件的读取问题
jinjiajie
2006-03-01 10:50:32
我写了一个读取bmp图片的程序,但是24位的图像都会在读几个字节后就read不出来了,read函数的返回值为-1,为什么会出现这种情况的?有什么办法能解决吗?
...全文
163
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
打赏
举报
回复
哎...头大了,没人和我一样遇到类似情况的吗?
java
读取
zip
文件
内容
读取
不到数据
问题
记录
·# 需求:需要将zip
文件
里面的
文件
进行拷贝,将拷贝后的
文件
上传到对应的服务器 遇到的
问题
,在
读取
zip
文件
里面的
文件
时,发现
读取
到的
文件
大小和实际
文件
大小不一致 相关代码如下: public class ZipFileDemp { @SneakyThrows public static void main(String[] args) { File fileTemp = new File("/Users/zhuifengzheng/Downloads/fi
Java工程
读取
resources中资源
文件
路径
问题
,jar中的
文件
无法
读取
Java工程
读取
resources中资源
文件
路径
问题
,jar中的
文件
无法
读取
问题
描述现象定位前言1、本地运行
读取
资源
文件
2、
读取
jar包中的
文件
信息
问题
描述 现象 作为一个刚开始学习java的新人,很多东西都是摸着石头过河,踩坑是常有的事。这不,今天我将maven管理的一个spring boot的WebAPP部署到服务器上,运行直接报错!纳尼!!!本地跑得好好的,一到服务器就出
问题
,关键是日志文...
idea
读取
文件
时的路径
问题
idea
读取
文件
时的路径
问题
先来看一段关于FileInputStream
读取
文件
的代码 package cn.zw.test; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class Test { public static void main(String[] args) { FileInputStream fi
c++
文件
读取
最后一行
读取
两次的
问题
c++
文件
读取
最后一行
读取
两次的
问题
新人博客。本来记笔记用word,写博客查阅更方便,资料也不容易丢失还能与人交流,也就转战博客了。在做c++
文件
流的题目时要求将20个数据从
文件
中
读取
到一个长度为20的向量中,我本来是这么写的: i=0; while(in.eof()==false) { if(in.bad()==false&&in.fail()==false)
java
读取
文件
到内存_Java解决大
文件
读取
的内存
问题
以及
文件
流的比较
Java解决大
文件
读取
的内存
问题
以及
文件
流的比较传统方式
读取
文件
的方式一般是是从内存中
读取
,官方提供了几种方式,如BufferedReader, 以及InputStream 系列的,也有封装好的如Guava和Apache commons IO提供了如下快速
读取
文件
的方式Files.readLines(new File(path), Charsets.UTF_8);FileUtils.readLin...
C语言
70,040
社区成员
243,237
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章