0
我想打开用户在屏幕上键入的bmp文件。我有一个函数,要求图像名称和另一个加载BMP图片:加载BMP文件时出错C
/**************************************************************************
* load image *
* Ask the user for an image file to load into screen. *
**************************************************************************/
void load_image(){
char name[100];
BITMAP image;
save_frame_buffer();
set_mode(TEXT_MODE);
printf("Enter the name of image (BMP): \n");
fgets(name,100,stdin);
set_mode(VGA_256_COLOR_MODE);
set_pallete(background.pallete);
load_bmp(name,&image);
show_buffer(frame_buffer);
draw_bitmap(&image,32,0);
}
/**************************************************************************
* load_bmp *
* Loads a bitmap file into memory. *
**************************************************************************/
void load_bmp(char *file, BITMAP *b){
FILE *fp;
long index;
word num_colors;
int x;
/*Trying to open the file*/
if((fp = fopen(file,"rb")) == NULL){
printf("Error opening the file %s.\n",file);
exit(1);
}
/*Valid bitmap*/
if(fgetc(fp) != 'B' || fgetc(fp) != 'M'){
fclose(fp);
printf("%s is not a bitmap file. \n", file);
exit(1);
}
/* Read and skip header
*/
fskip(fp,16);
fread(&b->width, sizeof(word),1 , fp);
fskip(fp,2);
fread(&b->height, sizeof(word),1,fp);
fskip(fp,22);
fread(&num_colors,sizeof(word),1,fp);
fskip(fp,6);
/* color number VGA -256 */
if(num_colors ==0) num_colors = 256;
/*Allocating memory*/
if((b->data = (byte *) malloc((word)(b->width*b->height))) == NULL)
{
fclose(fp);
printf("Error allocating memory for file %s.\n",file);
exit(1);
}
/*Reading pallete information*/
for(index=0;index<num_colors;index++){
b->pallete[(int)(index*3+2)] = fgetc(fp) >> 2;
b->pallete[(int)(index*3+1)] = fgetc(fp) >> 2;
b->pallete[(int)(index*3+0)] = fgetc(fp) >> 2;
x = fgetc(fp);
}
/*Reading the bitmap*/
for(index=(b->height-1)*b->width;index>=0;index-=b->width){
for(x=0;x<b->width;x++){
b->data[(word)(index+x)] = (byte) fgetc(fp);
}
}
fclose(fp);
}
的load_bmp()
功能工作正常,因为从来就成功加载其他图像。我面临的问题是输入。
当我硬编码的文件名是这样的:
load_bmp("mainbar.bmp",&image);
BMP文件被成功加载。但是,如果将name
变量放在load_bmp()
函数中,我会将fp
设置为NULL
。
任何人都可以告诉我是什么导致了这个问题?
这就是我需要的。我没有阅读手册页,我很抱歉。非常感谢您的帮助。 –
没问题。对于这种情况,手册中通常有一个隐藏的宝石。 :)在这种情况下,换行符经常被忽略,所以你并不孤单! – lurker