Archived

This topic is now archived and is closed to further replies.

markty

can't read file all the way

Recommended Posts

Okay I am trying to read this 3ds file all the way but I am able to only do one object and then reaches the end of the file. I can't find out how or why this is doing it maybe I am reading it wrong but I am able to get the data out of the first object in the beggening of it but after I read it it skips the rest and I was hoping someone could show me where the error might be becuase the code looks fine.
	if (!(modelfile=fopen(mfile, "rb")))
	{
		MessageBox(NULL,"Unable to load file!","ERROR",MB_OK|MB_ICONEXCLAMATION);
	}
	else
	{
		fseek(modelfile, 0, SEEK_END);
		size=ftell(modelfile);
		fseek(modelfile, 0, SEEK_SET);
	}

	while (ftell(modelfile)<size)
	{
		fread (&chunkID, 2, 1, modelfile);			//Read what chunk is going to be read next

		fread (&chunkLength, 4, 1, modelfile);		//Read the lenght of the chunk 


		switch (chunkID)
		{
			case 0x4d4d:		//main

			break;

			case 0x3d3d:		//3dmodel info

			break;

			case 0x4000:		//object name

			name=NULL;
			while (name = fgetc(modelfile) != 0);
			break; 

			case 0x4100:    //OBJ_TRIMESH

			break;

			case 0x4110: 
			fread (&vertlist.count, 2, 1, modelfile);
			for (vertloop=0; vertloop<vertlist.count; vertloop++)
			{
				fread (&vertlist.vertexinfo.vert[vertloop].x, 4, 1, modelfile);
				fread (&vertlist.vertexinfo.vert[vertloop].y, 4, 1, modelfile);
				fread (&vertlist.vertexinfo.vert[vertloop].z, 4, 1, modelfile);
			}
			break; 

			case 0x4120:
			fread (&vertlist.facecount, 2, 1, modelfile);
			for (polyloop=0; polyloop<vertlist.facecount; polyloop++)
			{
				fread (&vertlist.vertexinfo.vert[polyloop].a, 2, 1, modelfile);
				fread (&vertlist.vertexinfo.vert[polyloop].b, 2, 1, modelfile);
				fread (&vertlist.vertexinfo.vert[polyloop].c, 2, 1, modelfile);

				fread (&faceinfo, 2, 1, modelfile);      
			}

			default:
				fseek(modelfile, chunkLength-6, SEEK_CUR);
		}
	}
	
	fclose (modelfile);
[edited by - markty on November 15, 2003 1:02:57 AM]

Share this post


Link to post
Share on other sites
I''d start by changing

if (!(modelfile=fopen(mfile, "rb")))
{
MessageBox(NULL,"Unable to load file!","ERROR",MB_OK|MB_ICONEXCLAMATION);
}
else
{
fseek(modelfile, 0, SEEK_END);
size=ftell(modelfile);
fseek(modelfile, 0, SEEK_SET);
}

while (ftell(modelfile)<size)


to

if (!(modelfile=fopen(mfile, "rb")))
{
MessageBox(NULL,"Unable to load file!","ERROR",MB_OK|MB_ICONEXCLAMATION);
return;
}

while (!feof(modelfile))


This part:

case 0x4000: //object name
name=NULL;
while (name = fgetc(modelfile) != 0);
break;

1) name will always end up being 0.
2) Using ; for the body of a loop is confusing, if you want an empty loop use {}.
2) If name is a pointer, I don''t think you know anything about how to use pointers.

I don''t think any of that will fix the original problem, though. For that, use a debugger. Step through the code, and check the values being read from the file to make sure they''re what you expected.

Some more tips:
- Check the return value of fread to make sure you read the correct amount of data.
- The data you''re reading is endian depenant; that is, depending in the computer you''re using, the bytes may end up being backwards, making the values you read wrong, and bad things will happen as a result.

Share this post


Link to post
Share on other sites