forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem with fopen very strange

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
Ghoti



Joined: 31 Dec 2005
Posts: 288

PostPosted: Thu Nov 23, 2006 9:53 pm    Post subject: Problem with fopen very strange Reply with quote

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:
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 (j=Correction;j<fc;j++)
               {
                  for (k=0; k<3; k++)
                  {
                     ObjMeshParts[iNumberOfGroups].Vertices[l].u = TexCoords[Faces[j].textc[k]].x;
                     ObjMeshParts[iNumberOfGroups].Vertices[l].v = -(TexCoords[Faces[j].textc[k]].y);
                     ObjMeshParts[iNumberOfGroups].Vertices[l].color = 0xffffffff;
                     ObjMeshParts[iNumberOfGroups].Vertices[l].x = Vertices[Faces[j].vertices[k]].x;
                     ObjMeshParts[iNumberOfGroups].Vertices[l].y = Vertices[Faces[j].vertices[k]].y;
                     ObjMeshParts[iNumberOfGroups].Vertices[l].z = Vertices[Faces[j].vertices[k]].z;
                     l++;
                  }
               }
               
               // the texture for this part.
               sprintf(sBuffer, "%s",Textures[iNumberOfGroups]);
               ObjMeshParts[iNumberOfGroups].texture = loadImage(sBuffer);
               if (ObjMeshParts[iNumberOfGroups].texture == NULL) { return -4; }
               
               // increment the group number.
               Correction = fc;
               iNumberOfGroups++;
            }
            else if (strncmp("v ", ReadBuffer, 2) == 0 )
            {
               sscanf((ReadBuffer+2), "%f%f%f",&Vertices[ vc ].x, &Vertices[ vc ].y, &Vertices[ vc ].z);
               vc++;
            }
            else if (strncmp("vt ", ReadBuffer, 3) == 0 )
            {
               sscanf((ReadBuffer+3), "%f%f",&TexCoords[ tc ].x, &TexCoords[ tc ].y);
               tc++;
            }

            else if (strncmp("f ", ReadBuffer, 2) == 0 )
            {
               
               char *pSplitString = NULL;
               int Waste;
               i=0;
               pSplitString = strtok((ReadBuffer+2)," \t\n");

               do {

                  sscanf((pSplitString), "%d/%d/%d",&Faces[ fc ].vertices[ i ], &Faces[ fc ].textc[ i ],&Waste);
                  Faces[ fc ].textc[ i ] -= 1;      // 1 down because the obj file objects start at 1 and arrays start at 0
                  Faces[ fc ].vertices[ i ] -= 1;
                  pSplitString = strtok(NULL," \t\n");
                  i += 1;
               }
               while( pSplitString );
               
               
               
               
               fc++;
            }
         }
         fclose(fp);



      return 0;
   };

_________________
My PSP games:

Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
Back to top
View user's profile Send private message
Ghoti



Joined: 31 Dec 2005
Posts: 288

PostPosted: Wed Nov 29, 2006 9:24 am    Post subject: Reply with quote

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?
_________________
My PSP games:

Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
Back to top
View user's profile Send private message
TyRaNiD



Joined: 18 Jan 2004
Posts: 918

PostPosted: Thu Nov 30, 2006 12:59 am    Post subject: Reply with quote

open or the PSP specific sceIoOpen coupled with read/write sceIoRead/sceIoWrite
Back to top
View user's profile Send private message
TUniqueW



Joined: 09 Dec 2006
Posts: 12

PostPosted: Sat Dec 09, 2006 3:12 pm    Post subject: Reply with quote

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!
Back to top
View user's profile Send private message MSN Messenger
hlide



Joined: 10 Sep 2006
Posts: 750

PostPosted: Sat Dec 09, 2006 6:53 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
dot_blank



Joined: 28 Sep 2005
Posts: 498
Location: Brasil

PostPosted: Sat Dec 09, 2006 9:51 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Ghoti



Joined: 31 Dec 2005
Posts: 288

PostPosted: Tue Dec 12, 2006 1:20 am    Post subject: Reply with quote

Hi,

i have tried this:
Code:
FILE *fp = NULL;
before the file that causes the error but it did not work.

I have also tried this:

Code:
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 :(
_________________
My PSP games:

Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group