File Read problem ?!

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

Moderators: cheriff, TyRaNiD

Post Reply
ShUr1k3n
Posts: 42
Joined: Sun Oct 16, 2005 9:04 pm

File Read problem ?!

Post by ShUr1k3n »

Hey guys....

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

Code: Select all


int Level[161];

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

   fich = fopen(levelName, "r");    

	for &#40;i=1;i<=160;i++&#41;
	&#123;		
		fscanf&#40;fich,"%d",&Level&#91;i&#93;&#41;;	
	&#125;
	
	fclose&#40;fich&#41;;
&#125;

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
fish
Posts: 25
Joined: Wed Feb 08, 2006 5:12 am

Post by fish »

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: Select all

int Level&#91;160&#93;;

void Read_Level&#40;char *levelName&#41;
&#123;
   int i;
   
   FILE *fich;

   fich = fopen&#40;levelName, 'r'&#41;;   

   for &#40;i=0;i<159;i++&#41;
   &#123;      
      fscanf&#40;fich,"%d",&Level&#91;i&#93;&#41;;   
   &#125;
   
   fclose&#40;fich&#41;;
&#125;
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); in the loop too, then you could see when the the program crashes (ie the last number shown when it freezes)
sg57
Posts: 144
Joined: Fri Oct 14, 2005 2:26 pm

Post by sg57 »

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 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?
Brunni
Posts: 186
Joined: Sat Oct 08, 2005 10:27 pm

Post by Brunni »

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: Select all

int Level&#91;160&#93;; 

void Read_Level&#40;char *levelName&#41; 
&#123; 
   int i; 
   FILE *fich; 

   fich = fopen&#40;levelName, "r"&#41;;
   if &#40;fich != NULL&#41;
   &#123;
       for &#40;i=1;i<=160;i++&#41;
       &#123;
          fscanf&#40;fich,"%d",&Level&#91;i&#93;&#41;;
       &#125;
       fclose&#40;fich&#41;;
   &#125;
&#125;
Sorry for my bad english
Image Oldschool library for PSP - PC version released
Warrick
Posts: 9
Joined: Tue Dec 06, 2005 6:49 am

Try to use Sce Functions

Post by Warrick »

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

I think this will help you:

Code: Select all

int Level&#91;160&#93;;
void Load_Levels&#40;char* filename&#41;
&#123;
    int i,fd;
    int* iPtr;
    long size;
 
    if &#40;&#40;fd = sceIoOpen&#40;filename, PSP_O_RDONLY, 0777&#41;&#41; > 0&#41; 
    &#123;
        size = sceIoLseek&#40;fd, 0, PSP_SEEK_END&#41;;
        sceIoLseek&#40;fd, 0, PSP_SEEK_SET&#41;;
        iPtr = &#40;unsigned char *&#41; malloc&#40;size+1&#41;; // You say 160 bytes!!! but 160 int mean 320 bytes!!!
        memset&#40;iPtr, 0, size + 1&#41;;
        if &#40;iPtr != 0&#41; sceIoRead&#40;fd, iPtr, size&#41;;
        else 
        &#123;
          printf&#40;"No Free Memory\n"&#41;;
          sceIoClose&#40;fd&#41;;
          return;
        &#125;
        sceIoClose&#40;fd&#41;;
        for &#40;i=0;&#40;i<size && i<160&#41;;i++&#41; Level&#91;i&#93; = &#40;int&#41; iPtr&#91;i&#93;;
        free&#40;iPtr&#41;;
    &#125;
&#125;
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
Orion_
Posts: 69
Joined: Thu Jan 27, 2005 8:47 am

Re: Try to use Sce Functions

Post by Orion_ »

Warrick wrote:Hello first of all, 160 integers are 320 bytes not 160 bytes.
I would say 640 bytes, not 320.
ShUr1k3n
Posts: 42
Joined: Sun Oct 16, 2005 9:04 pm

Post by ShUr1k3n »

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: Select all

int Level&#91;160&#93;; 

void Read_Level&#40;char *levelName&#41; 
&#123; 
   int i; 
   FILE *fich; 

   fich = fopen&#40;levelName, "r"&#41;;
   if &#40;fich != NULL&#41;
   &#123;
       for &#40;i=1;i<=160;i++&#41;
       &#123;
          fscanf&#40;fich,"%d",&Level&#91;i&#93;&#41;;
       &#125;
       fclose&#40;fich&#41;;
   &#125;
&#125;
If u look at MY code (1st POST) u will see that i put "r" and not 'r'...

So the problem isn't that...
Warrick
Posts: 9
Joined: Tue Dec 06, 2005 6:49 am

Re: Try to use Sce Functions

Post by Warrick »

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
DustinFraze
Posts: 13
Joined: Fri Jan 06, 2006 5:44 pm

Post by DustinFraze »

Wrong topic. Disreguard.
Post Reply