what's wrong withe the code? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

what's wrong withe the code?

tapansinghnayytapansinghnayy Posts: 2Member
I wrote a program in C to open multiple bitmap images and check that they have the same dimensions. It compiles successfully but during execution the program stops unexpectedly.
what's wrong withe the code?

code:

typedef unsigned short word;
typedef unsigned char byte;

typedef struct tagBITMAP /* the structure for a bitmap. */
{
word width[50];
word height[50];
byte *data;
} BITMAP;

void fskip(FILE *fp, int num_bytes)
{
int i;
for (i=0; i<num_bytes; i++)
fgetc(fp);
}

int main()
{
BITMAP bmp;
char path[100]; //String to save the path of the file.
FILE *fp[50]; //file pointer array : stores the pointers to the files selected that are to be joined.
char choice;
int flag = 1; //Variable to control the while loop.
int count = 0; //Specifies the number of files selected.

while(flag)
{
/*
Read in full path of a file.
*/
printf("

Enter full path of a file : ");
scanf("%s",&path);
fp[count] = fopen(path,"r");

if(fgetc(fp[count])=='B' && fgetc(fp[count])=='M'){
count++;
printf("

Number of images added : %d",count);

if (count==1){
fskip(fp[0],16);
fread(&bmp.width[0], sizeof(word), 1, fp[0]);
fskip(fp[0],2);
fread(&bmp.height[0], sizeof(word), 1, fp[0]);
printf("

Dimensions of image no %d :
width = %d height = %d",count,bmp.width[0],bmp.height[0]);
}
else {
int temp = 0;
temp = count - 1;
fskip(fp[temp],16);
fread(&bmp.width[temp], sizeof(word), 1, fp[temp]);
fskip(fp[temp],2);
fread(&bmp.height[temp], sizeof(word), 1, fp[temp]);

printf("

Dimensions of image no %d :
width = %d height = %d",count,bmp.width[temp],bmp.height[temp]);

if ((bmp.height[0] != bmp.height[temp]) || (bmp.width[0] != bmp.width[temp])){
count = count-1;
printf("

Dimension mismatch");
printf("
Please enter image of same dimension as of first image.");
printf("
Do you wih to enter image (y/n) :");
scanf("%s",&choice);

if(choice == 'y'){
continue;
}
else{
flag = 0;
exit(0);

}
}
}
printf("

Do you wish to add another image (y/n) : ");
scanf("%s",&choice);

if(choice == 'y' || choice=='Y'){
continue;
}

else if (count==1 && (choice=='n' || choice=='N')){
printf("

Please enter 2 or more images to proceed.");
printf("

Do you wish to add another image (y/n) : ");
scanf("%s",&choice);

if(choice == 'y'){
continue;
}
else{
flag = 0;
exit(0);
}
}
else{
break;
}

}
}
getch();
}
Sign In or Register to comment.