Problem with fopen very strange

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

Problem with fopen very strange

Post by Ghoti »

Hi folks,

my function Load returns -3 as a value, this is returned when I try to open the file "object", the strange thing however is that it opens the file also with then return -1 if reads fails but there it does not fail,

what am i doing wrong? or is it not related to the read?

Code: Select all

int Obj::Load(const char *object, const char *material) {

			char ReadBuffer[256];
			char Textures[200][256];
			char sBuffer[200];
			unsigned int vc=0,nc=0,tc=0, fc=0;
			unsigned int i=0;
			unsigned int j,k,l;
			unsigned int iNumberOfFaces = 0 ;
			unsigned int iNumberOfVertices = 0;
			unsigned int iNumberOfTexCoords = 0;
			unsigned int iNumberOfTextures = 0;
			unsigned int iNumberOfGroups = 0;
			iNumberOfFaces = 0;

			// read the object file
			FILE *fp = NULL;
			if ((fp = fopen(object, "rb")) == NULL) { return -1; }
			while(!feof(fp))
			{
				fgets(ReadBuffer, 256, fp);
				if (strncmp("g default", ReadBuffer, 9) == 0 ) { iNumberOfGroups++;}
				else if (strncmp("v ", ReadBuffer, 2) == 0 ) { iNumberOfVertices++; }
				else if (strncmp("vt ", ReadBuffer, 3) == 0 ) { iNumberOfTexCoords++; }
				else if (strncmp("f ", ReadBuffer, 2) == 0 ) { iNumberOfFaces++; }
			}
			fclose(fp);

			// read the textures:
			iNumberOfTextures = 0;

			if ((fp = fopen(material, "rb")) == NULL) { return -2; }
			while(!feof(fp)) {
				fgets(ReadBuffer, 256, fp);
				if (strncmp("map_Kd ", ReadBuffer, 7) == 0 ) {
					sprintf(Textures[iNumberOfTextures],"Textures/%s", strtok((ReadBuffer+7),"#"));
					iNumberOfTextures++;
				}

			}
			fclose(fp);

			ScePspFVector3	Vertices[ iNumberOfVertices ];
			ScePspFVector2	TexCoords[ iNumberOfTexCoords ];
			ObjFaceNew		Faces[ iNumberOfFaces ];


			iNumberOfParts = iNumberOfGroups;


			ObjMeshParts = (ObjMeshPart*)malloc(iNumberOfGroups * sizeof(ObjMeshPart));

			unsigned int Correction = 0;
			iNumberOfGroups = 0;

			vc=0;
			nc=0;
			tc=0;
			fc=0;

			if ((fp = fopen(object, "rb")) == NULL) { return -3; }
			while(!feof(fp))
			{
				fgets(ReadBuffer, 256, fp);
				if (strncmp("EndGroup", ReadBuffer, 8) == 0 )
				{
					

					ObjMeshParts[iNumberOfGroups].Vertices = (ObjVertexB*)malloc((fc-Correction) * 3 * sizeof(ObjVertexB));
					//tmpObj->ObjMeshPart[iNumberOfGroups].iNumberOfFaces = fc - Correction;
					ObjMeshParts[iNumberOfGroups].iFaces = (fc-Correction);
					//tmpObj->iNumberOfFaces[iNumberOfGroups] = (fc-Correction);
					l = 0;

					for &#40;j=Correction;j<fc;j++&#41;
					&#123;
						for &#40;k=0; k<3; k++&#41;
						&#123;
							ObjMeshParts&#91;iNumberOfGroups&#93;.Vertices&#91;l&#93;.u = TexCoords&#91;Faces&#91;j&#93;.textc&#91;k&#93;&#93;.x;
							ObjMeshParts&#91;iNumberOfGroups&#93;.Vertices&#91;l&#93;.v = -&#40;TexCoords&#91;Faces&#91;j&#93;.textc&#91;k&#93;&#93;.y&#41;;
							ObjMeshParts&#91;iNumberOfGroups&#93;.Vertices&#91;l&#93;.color = 0xffffffff;
							ObjMeshParts&#91;iNumberOfGroups&#93;.Vertices&#91;l&#93;.x = Vertices&#91;Faces&#91;j&#93;.vertices&#91;k&#93;&#93;.x;
							ObjMeshParts&#91;iNumberOfGroups&#93;.Vertices&#91;l&#93;.y = Vertices&#91;Faces&#91;j&#93;.vertices&#91;k&#93;&#93;.y;
							ObjMeshParts&#91;iNumberOfGroups&#93;.Vertices&#91;l&#93;.z = Vertices&#91;Faces&#91;j&#93;.vertices&#91;k&#93;&#93;.z;
							l++;
						&#125;
					&#125;
					
					// the texture for this part.
					sprintf&#40;sBuffer, "%s",Textures&#91;iNumberOfGroups&#93;&#41;; 
					ObjMeshParts&#91;iNumberOfGroups&#93;.texture = loadImage&#40;sBuffer&#41;;
					if &#40;ObjMeshParts&#91;iNumberOfGroups&#93;.texture == NULL&#41; &#123; return -4; &#125;
					
					// increment the group number.
					Correction = fc;
					iNumberOfGroups++;
				&#125;
				else if &#40;strncmp&#40;"v ", ReadBuffer, 2&#41; == 0 &#41;
				&#123;
					sscanf&#40;&#40;ReadBuffer+2&#41;, "%f%f%f",&Vertices&#91; vc &#93;.x, &Vertices&#91; vc &#93;.y, &Vertices&#91; vc &#93;.z&#41;; 
					vc++;
				&#125;
				else if &#40;strncmp&#40;"vt ", ReadBuffer, 3&#41; == 0 &#41;
				&#123;
					sscanf&#40;&#40;ReadBuffer+3&#41;, "%f%f",&TexCoords&#91; tc &#93;.x, &TexCoords&#91; tc &#93;.y&#41;; 
					tc++;
				&#125;

				else if &#40;strncmp&#40;"f ", ReadBuffer, 2&#41; == 0 &#41;
				&#123;
					
					char *pSplitString = NULL;
					int Waste;
					i=0;
					pSplitString = strtok&#40;&#40;ReadBuffer+2&#41;," \t\n"&#41;;

					do &#123;

						sscanf&#40;&#40;pSplitString&#41;, "%d/%d/%d",&Faces&#91; fc &#93;.vertices&#91; i &#93;, &Faces&#91; fc &#93;.textc&#91; i &#93;,&Waste&#41;; 
						Faces&#91; fc &#93;.textc&#91; i &#93; -= 1;		// 1 down because the obj file objects start at 1 and arrays start at 0
						Faces&#91; fc &#93;.vertices&#91; i &#93; -= 1;
						pSplitString = strtok&#40;NULL," \t\n"&#41;;
						i += 1;
					&#125;
					while&#40; pSplitString &#41;;
					
					
					
					
					fc++;
				&#125;
			&#125;
			fclose&#40;fp&#41;;



		return 0;
	&#125;;
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

Post by Ghoti »

Hmmm well i don't really know what is wrong with this but is there an other function then fopen to use to open files and read them?
TyRaNiD
Posts: 907
Joined: Sun Jan 18, 2004 12:23 am

Post by TyRaNiD »

open or the PSP specific sceIoOpen coupled with read/write sceIoRead/sceIoWrite
User avatar
TUniqueW
Posts: 12
Joined: Sat Dec 09, 2006 12:51 pm

Post by TUniqueW »

if TyRaNiD's help didn't work -->
try setting the fp buffer to NULL or zero or w/e before opening each file that may be a problem (who knows) ..... or use three different buffers its always a good resolve for file problems,

Best,
TUW
Developer: C/C++ knows nothing about assembly!
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

a little off topic : do functions like fopen/fwrite/fread/fclose have some issues with standby mode because opened files are closed and those functions are not aware about this case ? I believe to have heard that somewhere in this forums. Using sceIoPen/sceIoWrite/sceIoRead/sceIoClose would work because they would be reopened when exiting standby mode.

If someone can confirm this issue or bring the counterproof, that would be a help for people to decide what to use : fopen or sceIoOpen.
User avatar
dot_blank
Posts: 498
Joined: Wed Sep 28, 2005 8:47 am
Location: Brasil

Post by dot_blank »

using fopen/fwrite/fread/fclose after standby mode
will not gurantee the file handles to point where they should
you must handle these after you come out of standby mode
10011011 00101010 11010111 10001001 10111010
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

Post by Ghoti »

Hi,

i have tried this:

Code: Select all

FILE *fp = NULL;
before the file that causes the error but it did not work.

I have also tried this:

Code: Select all

FILE *fb = NULL;
but this does also not work.

and the strange this is that i use to do it that way in C with the same code except that a struct was passed to the function so that that struct could be filled instead of this object using c++ but with the C code it worked fine and now it doesn't :(
Post Reply