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 

File Read problem ?!

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



Joined: 16 Oct 2005
Posts: 42

PostPosted: Wed Mar 08, 2006 8:06 am    Post subject: File Read problem ?! Reply with quote

Hey guys....

Can any1 plz tell me what the problem with this code ?!

Code:


int Level[161];

void Read_Level(char levelName[255])
{
   int i;
   
   FILE *fich;

   fich = fopen(levelName, "r");   

   for (i=1;i<=160;i++)
   {      
      fscanf(fich,"%d",&Level[i]);   
   }
   
   fclose(fich);
}



The File contain 160 numbers (between 0 and 40) in each LINE.

When the program goes into this code the PSP freezes.. And restart...

Whats the problem ?!

:S
Back to top
View user's profile Send private message Send e-mail
fish



Joined: 08 Feb 2006
Posts: 25

PostPosted: Wed Mar 08, 2006 11:45 am    Post subject: Reply with quote

not a very exact reply, but there's some things I'd do differently, whether they make a difference or not I'm not sure...

Code:

int Level[160];

void Read_Level(char *levelName)
{
   int i;
   
   FILE *fich;

   fich = fopen(levelName, 'r');   

   for (i=0;i<159;i++)
   {     
      fscanf(fich,"%d",&Level[i]);   
   }
   
   fclose(fich);
}


I imagine the problem is in the for loop, you go from 1 to 160, which is more than you need, 0-159 if the file only contains 160 chars then at the end of the loop you're trying to read a number that doesn't exist.

I'd also put 'r' in single quotes as its a char, not a string
and I'd use char *levelName because thats how I normally pass file names to functions.

You could always include a printf("%d,"Level[i]); in the loop too, then you could see when the the program crashes (ie the last number shown when it freezes)
Back to top
View user's profile Send private message
sg57



Joined: 14 Oct 2005
Posts: 154

PostPosted: Wed Mar 08, 2006 11:50 am    Post subject: Reply with quote

Well, Im not sure if this is right but make sure you are using a char (buffer) when using this.

Also, it is not neccesary to have the char levelName[255] with the [255]. You may also be overflowing the buffer, I cant remember but does the scanf search/rea/make the \0 ?

Also, you may want to set the int 'i' to an actual number, not just make it a variable capable of manipulating because right when starting itll read it and see scanf for Level[i] when i is nothing, so simple make the integer i = 0. or -1, depending if 0 is a number in your file.

Also, use the itoa function instead of the scanf, it is just like scanf except itoa only looks for integers and feeds them into a buffer easy for use.

Im not saying anything is worng, but if your stuck, maybe try something that i stated?
Back to top
View user's profile Send private message
Brunni



Joined: 08 Oct 2005
Posts: 186

PostPosted: Wed Mar 08, 2006 8:02 pm    Post subject: Reply with quote

There are two reasons that can make your program crash:
1) fopen(levelName, 'r') -> "r" (this is certainly what made your PSP freeze). 'r' is a char, and fopen needs a string (char*), these are incompatible.
2) verify that the file could be open, else it will crash if the file doesn't exist.
Code:
int Level[160];

void Read_Level(char *levelName)
{
   int i;
   FILE *fich;

   fich = fopen(levelName, "r");
   if (fich != NULL)
   {
       for (i=1;i<=160;i++)
       {
          fscanf(fich,"%d",&Level[i]);
       }
       fclose(fich);
   }
}

_________________
Sorry for my bad english
Oldschool library for PSP - PC version released
Back to top
View user's profile Send private message
Warrick



Joined: 06 Dec 2005
Posts: 9

PostPosted: Wed Mar 08, 2006 8:54 pm    Post subject: Try to use Sce Functions Reply with quote

Hello first of all, 160 integers are 320 bytes not 160 bytes.

I think this will help you:

Code:

int Level[160];
void Load_Levels(char* filename)
{
    int i,fd;
    int* iPtr;
    long size;
 
    if ((fd = sceIoOpen(filename, PSP_O_RDONLY, 0777)) > 0)
    {
        size = sceIoLseek(fd, 0, PSP_SEEK_END);
        sceIoLseek(fd, 0, PSP_SEEK_SET);
        iPtr = (unsigned char *) malloc(size+1); // You say 160 bytes!!! but 160 int mean 320 bytes!!!
        memset(iPtr, 0, size + 1);
        if (iPtr != 0) sceIoRead(fd, iPtr, size);
        else
        {
          printf("No Free Memory\n");
          sceIoClose(fd);
          return;
        }
        sceIoClose(fd);
        for (i=0;(i<size && i<160);i++) Level[i] = (int) iPtr[i];
        free(iPtr);
    }
}


I think that you have to change int Level[160] for unsigned char Level[160] if you want to use 1 byte for data (from 0-255 values).

I hope this help.

Kind Regards,
Warrick
Back to top
View user's profile Send private message
Orion_



Joined: 27 Jan 2005
Posts: 69

PostPosted: Wed Mar 08, 2006 9:09 pm    Post subject: Re: Try to use Sce Functions Reply with quote

Warrick wrote:
Hello first of all, 160 integers are 320 bytes not 160 bytes.

I would say 640 bytes, not 320.
Back to top
View user's profile Send private message
ShUr1k3n



Joined: 16 Oct 2005
Posts: 42

PostPosted: Thu Mar 09, 2006 2:26 am    Post subject: Reply with quote

Brunni wrote:
There are two reasons that can make your program crash:
1) fopen(levelName, 'r') -> "r" (this is certainly what made your PSP freeze). 'r' is a char, and fopen needs a string (char*), these are incompatible.
2) verify that the file could be open, else it will crash if the file doesn't exist.
Code:
int Level[160];

void Read_Level(char *levelName)
{
   int i;
   FILE *fich;

   fich = fopen(levelName, "r");
   if (fich != NULL)
   {
       for (i=1;i<=160;i++)
       {
          fscanf(fich,"%d",&Level[i]);
       }
       fclose(fich);
   }
}


If u look at MY code (1st POST) u will see that i put "r" and not 'r'...

So the problem isn't that...
Back to top
View user's profile Send private message Send e-mail
Warrick



Joined: 06 Dec 2005
Posts: 9

PostPosted: Thu Mar 09, 2006 2:38 am    Post subject: Re: Try to use Sce Functions Reply with quote

Orion_ wrote:
Warrick wrote:
Hello first of all, 160 integers are 320 bytes not 160 bytes.

I would say 640 bytes, not 320.


Yes, sorry for confussion i have short int on my mind ;).

Regards,
Warrick
Back to top
View user's profile Send private message
DustinFraze



Joined: 06 Jan 2006
Posts: 13

PostPosted: Thu Mar 09, 2006 6:17 pm    Post subject: Reply with quote

Wrong topic. Disreguard.
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