www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/11/23/09:52:03

From: Faulkner <faulkner AT southcom DOT com DOT au>
Newsgroups: comp.os.msdos.djgpp
Subject: strange file writing/reading problem
Date: Sat, 23 Nov 1996 03:20:17 +1100
Organization: Southern Internet Services
Lines: 181
Message-ID: <3295D2C1.4D5@southcom.com.au>
Reply-To: faulkner AT southcom DOT com DOT au
NNTP-Posting-Host: pa0d.hobart.southcom.com.au
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

This is a multi-part message in MIME format.

--------------2EE3B9415CE
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Hi

	Here's a problem for the DJGPP people of the world.
I've got 2 structures, a VertexType and a PolygonType, both are simple
and short structures. I write 20 vertices to a file, then 20 polygons
to a file. When I read the file back into memory any element that
has the no. 13 in it goes screwy!!!! (dont worry, I've checked for
viruses).

If for example each polygon has the values i,i,i the 14th polygon
(the one with 13,13,13) screws up. If I make it so the ith polygon
has the values i+2, i+2, i+2, then its the 12th polygon that screws
up.

Could someone please look at the attached code and tell me what the
hell is happening???? I've tried it on different machines, and its
only with DJGPP that it screws up.


						Ken Faulkner
(please email me the response aswell, since I dont always get the 
chance to read newgroups. )

--------------2EE3B9415CE
Content-Type: text/plain; charset=us-ascii; name="Test.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="Test.c"

#include <stdio.h>
#include <math.h>
#include <stdlib.h>


typedef unsigned char byte;

/* simple structures. */
typedef struct
{
	float Vertex[3];
} VertexType, *VertexTypePtr;

typedef struct
{
	int Vertex[3];
	byte Colour;	
	byte Pad[3];

} PolygonType, *PolygonTypePtr;


void main(int argc, char *argv[])
{
	int Temp;
	VertexTypePtr V;
	PolygonTypePtr P;
	int NoP, NoV, i, No;
	VertexType VTemp;
	PolygonType PTemp;
	char Inputs;
	float V1, V2, V3;
	int P1, P2, P3;
	byte Colour;
	char Pad[3];


	FILE *fp;

	fp=fopen(argv[1], "wb");
	if (fp==NULL)
	{
		printf("SHIT\n");
		exit(1);
	}

	No = 20;

	V = (VertexTypePtr) malloc(sizeof(VertexType)*No);
	P = (PolygonTypePtr) malloc(sizeof(PolygonType)*No);
	if ((V==NULL) || (P==NULL))
	{
		printf("memory problem.\n");
		exit(1);
	}

	/* Defines Vertices and polygons. */
	for (i=0;i<No;i++)
	{

	V[i].Vertex[0] = (float) i;
	V[i].Vertex[1] =(float) i ;
	V[i].Vertex[2] =(float) i ;
	P[i].Vertex[0] = i;
	P[i].Vertex[1] = i;
	P[i].Vertex[2] = i; 
	P[i].Colour = (byte) i;
	P[i].Pad[0] = 0;
	P[i].Pad[1] = 0;
	P[i].Pad[2] = 0;
	}

	for (i=0;i<No;i++)
	{
		printf("%f %f %f\n%d %d %d %d\n", 
			V[i].Vertex[0] ,
			V[i].Vertex[1],
			V[i].Vertex[2],
			P[i].Vertex[0],
			P[i].Vertex[1],
			P[i].Vertex[2],
			(int) P[i].Colour  );
	}


	/* First no. is no of vertices, second is no of polygons. In this
	 * case they are the same. 
	 */
	fwrite(&No, sizeof(int), 1, fp);
	fwrite(&No, sizeof(int), 1, fp);

	/* Write the vertices and polygons. */
	fwrite(V, sizeof(VertexType), No, fp);
	fwrite(P, sizeof(PolygonType), No, fp);

	fclose(fp);

	free(V);
	free(P);

	/* Read them in again. */
	fp=fopen(argv[1], "r");
	if (fp==NULL)
	{
		printf("SHIT\n");
		exit(1);
	}

	/* Read no vertices and no polygons. */
	fread(&NoV, sizeof(int), 1,fp);
	fread(&NoP, sizeof(int), 1,fp);
	printf("Read %d V's and %d P's\n", NoV, NoP);

	V = (VertexTypePtr) malloc(sizeof(VertexType)*NoV);
	P = (PolygonTypePtr) malloc(sizeof(PolygonType)*NoP);
	if ((V==NULL) || (P==NULL))
	{
		printf("memory problem.\n");
		exit(1);
	} 

	fread(V, sizeof(VertexType), NoV,  fp);
	fread(P, sizeof(PolygonType), NoP, fp);

	for (i=0;i<NoV;i++)
	{
		printf("%d: %f, %f, %f\n", i, V[i].Vertex[0], 
				V[i].Vertex[1],
				V[i].Vertex[2]);
	}

	for (i=0;i<NoP;i++)
	{
		printf("%d: %d, %d, %d\n", i, 
			P[i].Vertex[0],
			P[i].Vertex[1],
			P[i].Vertex[2]);
	}

	fclose(fp);
	free(V);
	free(P);

}

--------------2EE3B9415CE--

- Raw text -


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