delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/07/09/20:05:40

From: <levity AT minn DOT net>
Newsgroups: comp.os.msdos.djgpp
Subject: General protection fault because of modulus?
Date: 9 Jul 1997 13:52:11 GMT
Organization: None
Lines: 96
Message-ID: <01bc8c6e$c991d480$3564a8c0@Kelly.ns1.uswest.net>
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

I am writing a program for the purpous of learing how to read binary files.
 I am using the modulus fo counting coloms for the out put but every time I
add the code to do this the program runs and then ends with a general
protection fault.  Any ideas why I am getting the general protection fault?
	Also for future reference should I include my code in a different way when
posting these questions?

#include	<stdio.h>
#include	<stdlib.h>
#include	".\bmphead.h"



int main(int argc, char *argv[])
{

	long pal[3][256];
	int ch, i;
	char name[30] = "kelly.bmp";
	BMPHEAD bmh;
	INFOHEAD bih;
	WIN3PAL palette;

	FILE *fbmp = fopen( name, "rb");
	if (fbmp == 0)
	{
		printf("Could not open file %s\n", name);
		return 0;
	}

	if( fread(&bmh, sizeof(bmh), 1, fbmp) != 1)
	{
		(void)printf("error reading file %s\n", name);
		return 0;
	}
	
	if (fread(&bih, sizeof(bih), 1, fbmp) != 1)
	{ 
		(void)printf("Error reading file %s\n", name);
		return 0;
	}
	
	if (bih.ColorsUsed > 16 && bih.BitsPerPixel == 8)
	{
		for(i = 0; i <= bih.ColorsUsed; i++)
		{	
			if(fread(&palette, sizeof(palette), 1, fbmp) != 1)
			{
				(void)printf("Error reading file %s\n", name);
				return 0;
			}
			pal[0][i] = palette.Blue;
			pal[1][i] = palette.Green;
			pal[2][i] = palette.Red;
			pal[3][i] = palette.Pad;
		}
	}
		

	(void)printf("type is%x\n", bmh.ImageFileType);
	(void)printf("size is %ld\n", bmh.FileSize);
	(void)printf("reserved1 is %u\n", bmh.Reserved1);
	(void)printf("reserved2 is %u\n", bmh.Reserved2);
	(void)printf("offset is %ld\n", bmh.ImageDataOffset);

	(void)printf("struct size %x\n", bih.HeaderSize);
	(void)printf("bitmap width %x\n", bih.ImageWidth);
	(void)printf("bitmap height %x\n", bih.ImageHeight);
	(void)printf("number of plains alwase 1 %x\n", bih.NumberOfImagePlanes);
	(void)printf("bits per pixel %x\n", bih.BitsPerPixel);
	(void)printf("compression flag %x\n", bih.Compression);
	(void)printf("image size %x\n", bih.SizeOfBitmap);
	(void)printf("horz resolution %x\n", bih.HorzResolution);
	(void)printf("Vert resolution %x\n", bih.VertResolution);
	(void)printf("color table size %x\n", bih.ColorsUsed);
	(void)printf("significant colors %x\n", bih.ColorsImportant);
	
	for(i = 0; i < 256; i++)
	{
		if (i % 5 == 0)
		{
			(void)printf("%ld,%ld,%ld,%ld |\n", pal[0][i], pal[1][i], pal[2][i],
pal[3][i]);
		}
		else
			(void)printf("%ld,%ld,%ld,%ld |", pal[0][i], pal[1][i], pal[2][i],
pal[3][i]);	
		
	}

	fclose(fbmp);
	return 0;
}
	

		

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019