Advertisement

fread() on Win98???

Started by November 29, 2000 07:41 AM
13 comments, last by MrSandman666 24 years, 1 month ago
If anybody of you has had a close look at the file handle structure (FILE datatype) which is used to read data from a file using fread(), you might have noticed a member variable called "_cnt". fread() seems to read data in blocks of 4096 bytes (not so shure whether it''s bytes). The _cnt variable keeps track of the position in that block and thus decrements while reading through the file. When it reaches zero (initial state is 4096) it gets reset to 4096. Or this is how it should be. I observed this behaviour on WinNT and it worked fine. But when I try to read the same file with exactly the same methods on Win98 the _cnt variable doesn''t get reset. And when _cnt is zero, fread() denials service. That means I can''t even read half of the file. If the description of that problem is kinda unclear please ask for further explanations. I''m known for weird and confusing lines of thought and I''m not even a native English speaker. Does anybody know what to do about this whole fread() thing on Windows 98? "Mr Sandman bring me a dream"
-----------------------------"Mr Sandman bring me a dream"
I just found out that the same problem occurs on WinNT when I run my proggy in release mode on MSVC++.

"Mr Sandman bring me a dream"

Edited by - MrSandman666 on November 29, 2000 12:06:35 PM
-----------------------------"Mr Sandman bring me a dream"
Advertisement
is this with binary or text files.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
I guess the FILE structure is not really defined in the ANSI C standard. Like handles in windows, save it, use it, but don''t care about what it is ! If I''m right, you are doing a very very bad thing by accessing this variable directly !
Tim--------------------------glvelocity.gamedev.netwww.gamedev.net/hosted/glvelocity
to jenova:
I use binary files.
Oh, and thinking isn''t really troublesome for me. My problem is to express my thoughts with words.

to tcs:
I''m not really accessing the variable directly. I''m just checking it via the debugger.

Oh, and to make it clear: the program only works in the debug mode of MSVC++ on WinNT. It doesn''t work in any other mode (standalone, release, etc.) or on any other operatiing system.

"Mr Sandman bring me a dream"
-----------------------------"Mr Sandman bring me a dream"
okay, well i tried using "fread" and "fwrite" in both "Release" and "Debug" mode, and they worked fine. however, i am running Win98 OSR1. you said this trouble was with WinNT. i could try the code tomorrow at work, cause we run NT 4.0 there. i am using VC++ 6.0 Enterprise Edition. maybe that function isn''t supported under Standard or Learning Edition under release mode or something? i don''t know i am grasping for straws. if you could write out a snippet of the code that isn''t working, i''ll take a look at it. have you tried upgrading to the service packs available from the MS site.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
Advertisement
Well, it only WORKS on WinNT in Debug mode. It does NOT work on Win98 and in all other modes than Debug.

How often did you use the fread() function on the same file, jenova? For me it only reads the first 4096 bytes fine. The problem is only that my file is a lot bigger than that.

ANd I don''t want to make the users of my program to download the service pack!



"Mr Sandman bring me a dream"
-----------------------------"Mr Sandman bring me a dream"
well. i tried it with a "0x80000" sized file, and it worked just fine. just one "fread", i use standard ANSI C functions where possible in all of my work on Win95/98 for portability. i''ve seen a problem like this on Win2K when i was working on my psx emu, it wouldn''t read the entire psx executable, but i was only using Win2K for about a week and haven''t tried it since. i don''t know, that''s a wierd one. i guess you should try using the CreateFile and ReadFile functions or macros.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
Something that might help us help you is if you post the *exact* segment of code that you are using...

Dire Wolf
direwolf@digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
Well, the *exact* segment of code is a function, which is 138 lines long. I told you the file I''m reading is a little larger.
it has a few loops in it, which are executed a couple of hundred times.

One single fread() function works fine for me too. Problems start after the first 300 or something (I didn''t count. could be much more or much less) fread() calls.

Anyways, I''ll just post the comlpete function here:

  bool model::ms3dLoadModel(char *FileName){		Header   header;	word     count;	word	 i;	// open the file for read only access	FILE *File = fopen(FileName,"rb");	// Hey, there is no file!	if(!File)	{		MessageBox(hWnd, "Error reading Model", ProgName, MB_OK);		return(false); 	}// read the header	fread(&header.id, sizeof(char), 10, File);	fread(&header.version, sizeof(int), 1, File);	// get the number of vertices we have for this model	fread(&count, sizeof(word), 1, File);// allocate memory for our vertices	Vertices = new Vertex[count];// now that we have space, get the vertices	for(i = 0; i < count; i++)	{		fread(&Vertices<i>.flags, sizeof(byte), 1, File);				fread(&Vertices[i].vertex, sizeof(float), 3, File);				fread(&Vertices[i].boneId, sizeof(char), 1, File);				fread(&Vertices[i].referenceCount, sizeof(byte), 1, File);	}// get the number of polygons	fread(&count, sizeof(word), 1, File);// allocate memory for our polygons	Triangles = new Triangle[count];// now that we have space, get the polygons	for(i = 0; i < count; i++)	{		fread(&Triangles[i].flags, sizeof(word), 1, File);		fread(&Triangles[i].vertexIndices, sizeof(word), 3, File);		fread(&Triangles[i].vertexNormals, sizeof(float[3]), 3, File);		fread(&Triangles[i].s, sizeof(float), 3, File);		fread(&Triangles[i].t, sizeof(float), 3, File);		fread(&Triangles[i].smoothingGroup, sizeof(byte), 1, File);		fread(&Triangles[i].groupIndex, sizeof(byte), 1, File);	}// get the number of groups	fread(&numGroups, sizeof(numGroups), 1, File);// allocate memory for the groups	Groups = new Group[numGroups];// get the groups	for(i = 0; i < numGroups; i++) 	{		fread(&Groups[i].flags, sizeof(byte), 1, File);		fread(&Groups[i].name, sizeof(char), 32, File);		fread(&Groups[i].numtriangles, sizeof(word), 1, File);				// at this point we have to allocate memory to store the traingel		// ..indices in as the number of indices is different for each group		Groups[i].triangleIndices = new word[Groups[i].numtriangles];				fread(&Groups[i].triangleIndices, sizeof(word), Groups[i].numtriangles, File);			fread(&Groups[i].materialIndex, sizeof(char), 1, File);	}// get the number of materials	fread(&count, sizeof(word), 1, File);// allocate memory for our materials	Materials = new Material[count];// now that we have space, get the materials	for(i = 0; i < count; i++) 	{		fread(&Materials[i].name , sizeof(char), 32, File);		fread(&Materials[i].ambient , sizeof(float), 4, File);		fread(&Materials[i].diffuse , sizeof(float), 4, File);		fread(&Materials[i].specular , sizeof(float), 4, File);		fread(&Materials[i].emissive , sizeof(float), 4, File);		fread(&Materials[i].shininess , sizeof(float), 1, File);		fread(&Materials[i].transparency , sizeof(float), 1, File);		fread(&Materials[i].mode , sizeof(char), 1, File);		fread(&Materials[i].texture , sizeof(char), 128, File);		fread(&Materials[i].alphamap , sizeof(char), 128, File);	}		if(File->_cnt == 0)		MessageBox(hWnd, "_cnt = 0", ProgName, MB_OK);// animation speed (Frames Per Second)	fread(&AnimationFPS, sizeof(float), 1, File);	// get the position in the animation where the model starts	fread(&CurrentTime, sizeof(float), 1, File);	// get the total number of keyframes	fread(&TotalFrames, sizeof(int), 1, File);		// get the number of joints	fread(&count, sizeof(word), 1, File);// allcoate memory for the joints	Joints = new Joint[count];	// get the joints	for(i = 0; i < count; i++) 	{		fread(&Joints[i].flags, sizeof(byte), 1, File);		fread(&Joints[i].name, sizeof(char), 32, File);		fread(&Joints[i].parentName, sizeof(char), 32, File);		fread(&Joints[i].rotation, sizeof(float), 3, File);		fread(&Joints[i].position, sizeof(float), 3, File);		fread(&Joints[i].numKeyFramesRot, sizeof(word), 1, File);		fread(&Joints[i].numKeyFramesTrans, sizeof(word), 1, File);				Joints[i].keyFramesRot = new keyframerot[Joints[i].numKeyFramesRot];		Joints[i].keyFramesTrans = new keyframepos[Joints[i].numKeyFramesTrans];				fread(Joints[i].keyFramesRot, sizeof(keyframerot), Joints[i].numKeyFramesRot, File);		fread(Joints[i].keyFramesTrans, sizeof(keyframepos), Joints[i].numKeyFramesTrans, File);	}		// Ok, we''re done, close the file	fclose(File);	// now that we have all the data, build a display list form it	GenerateList();		return(true);} // end ms3dLoadModel()  


As always, if anything is unclear, go ahead and ask.

"Mr Sandman bring me a dream"
-----------------------------"Mr Sandman bring me a dream"

This topic is closed to new replies.

Advertisement